# Convert JSON Object to string

**URL:** <https://discuss.elastic.co/t/convert-json-object-to-string/72952>\
**Category:** Logstash\
**Created:** [January 26, 2017, 9:22pm UTC](https://discuss.elastic.co/t/convert-json-object-to-string/72952 "2017-01-26T21:22:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrick.wall](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/patrick.wall/32/15256_2.png) [@patrick.wall](https://discuss.elastic.co/u/patrick.wall)\
**Post date:** [January 26, 2017, 9:22pm UTC](https://discuss.elastic.co/t/convert-json-object-to-string/72952/1 "2017-01-26T21:22:42Z")

</div>

Hi All,

I am struggling to grasp something here. My use case is this - i am using logstash and elasticsearch to log every request that comes through my API. Whenever my api processes a request, I log the headers, and the json body of the response along with some metadata ( clientip, url, etc.) and send it to logstash in json format.

What is happening is Elasticsearch is indexing each element of the json body into a field instead of indexing the body into one field. What I would like is to have one field in elasticsearch APILog.res.body that contains in the json represented as a string. Is there a way I can convert the APILog.res.body object into a string? I cannot seem to make it work with mutate - I have the following filters to no effect. Can anyone provide some guidance on how to achieve this?

```
mutate {
  convert => {"%{[APILog][res][body]}" => "string"}
}

mutate {
  replace => {"%{[APILog][res][body]}" => "[APILog][resp][body]"}
}

```

Example Log

"APILog": {  
"res": {  
"statusCode": 200,  
"body": {  
"MyAPiResponse": {  
"Results": {  
"Results": [  
{  
"Result1": "1E6C7B9670A60331994D005056BD0622",  
"Result2": "160820",  
"Result3": "ACTV"  
}  
]  
}  
}  
}  
},  
"clientip": "10.0.0.0",  
"header": "my header"  
}

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 26, 2017, 9:30pm UTC](https://discuss.elastic.co/t/convert-json-object-to-string/72952/2 "2017-01-26T21:30:27Z")

</div>

That's a mapping issue, you will want to use the keyword type on the field so it doesn't split it up 🙂

---

<div class="post-metadata">

**Author:** ![patrick.wall](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/patrick.wall/32/15256_2.png) [@patrick.wall](https://discuss.elastic.co/u/patrick.wall)\
**Post date:** [January 26, 2017, 9:42pm UTC](https://discuss.elastic.co/t/convert-json-object-to-string/72952/3 "2017-01-26T21:42:37Z")

</div>

My good man, I am using a dynamic mapping right now. I actually just fixed this (five minutes after posting) by using the json\_encode filter.

```
json_encode {
  source => "[APILog][res][body]"
}

```

This is know logging exactly how i want. I will check out the keyword type when I go to define my own mapping template. I had not heard of that, thank you

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 23, 2017, 9:43pm UTC](https://discuss.elastic.co/t/convert-json-object-to-string/72952/4 "2017-02-23T21:43:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
