# Convert escaped-json to json

**URL:** https://discuss.elastic.co/t/convert-escaped-json-to-json/81387
**Category:** Logstash
**Created:** [April 6, 2017, 1:15am UTC](https://discuss.elastic.co/t/convert-escaped-json-to-json/81387 "2017-04-06T01:15:28Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ikirani](https://avatars.discourse-cdn.com/v4/letter/i/a9adbd/32.png) [@ikirani](https://discuss.elastic.co/u/ikirani)
#### Post date: [April 6, 2017, 1:15am UTC](https://discuss.elastic.co/t/convert-escaped-json-to-json/81387/1 "2017-04-06T01:15:28Z")

</div>

Hi,

One field in an incoming message is escaped-json-string. I need to convert it to JSON in logstash. What's the best way to do this?

I've tried the JSON filter as below:  
json {  
source =\> "message"  
add\_field =\> { "filtered" =\> "true" }  
}

The above didn't work for me.

Here is a sample input:

{  
"candidateID" : "12345",  
"category" : "Entry Level",  
"message" : "{\r\n "firstName": "John",\r\n "lastName": "Smith",\r\n "age": 25,\r\n "address":\r\n {\r\n "streetAddress": "21 2nd Street",\r\n "city": "New York",\r\n "state": "NY",\r\n "postalCode": "10021"\r\n },\r\n "phoneNumber":\r\n [\r\n {\r\n "type": "home",\r\n "number": "212 555-1234"\r\n },\r\n {\r\n "type": "fax",\r\n "number": "646 555-4567"\r\n }\r\n]\r\n }"

}

I've explored json filter and json\_lines. I tried the codec too. No luck yet. How do I convert the escaped json string in "message" field back to json before persisting in ElasticSearch?

Your response is appreciated.

---

<div class="post-metadata">

### Author: ![ikirani](https://avatars.discourse-cdn.com/v4/letter/i/a9adbd/32.png) [@ikirani](https://discuss.elastic.co/u/ikirani)
#### Post date: [April 6, 2017, 10:54pm UTC](https://discuss.elastic.co/t/convert-escaped-json-to-json/81387/2 "2017-04-06T22:54:35Z")

</div>

Answering my own question as I found a solution:  
I had to remove the \r, \n in mutate filter (gsub) before pushing the field through json filter.

---

<div class="post-metadata">

### Author: ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)
#### Post date: [April 7, 2017, 10:32am UTC](https://discuss.elastic.co/t/convert-escaped-json-to-json/81387/3 "2017-04-07T10:32:19Z")

</div>

What error did you get?

---

<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: [May 5, 2017, 10:44am UTC](https://discuss.elastic.co/t/convert-escaped-json-to-json/81387/4 "2017-05-05T10:44:19Z")

</div>

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