# Access json fields from string

**URL:** <https://discuss.elastic.co/t/access-json-fields-from-string/103538>\
**Category:** Logstash\
**Created:** [October 11, 2017, 11:44am UTC](https://discuss.elastic.co/t/access-json-fields-from-string/103538 "2017-10-11T11:44:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zuokuok](https://avatars.discourse-cdn.com/v4/letter/z/c6cbf5/32.png) [@zuokuok](https://discuss.elastic.co/u/zuokuok)\
**Post date:** [October 11, 2017, 11:44am UTC](https://discuss.elastic.co/t/access-json-fields-from-string/103538/1 "2017-10-11T11:44:54Z")

</div>

I have a valid string within my initial message.

I extract this string in a variable "payload" and remove the end of the string which is not part of the json.  
At this point, my string is a valid JSON.

```
                            mutate {
				gsub => [
					"payload", "end_of_string", ""
				]			
			}

```

I need to access fields values (also be able to fetch a whole object) and put these in other fields.

For example:

{  
"first": "one",  
"second": {  
"number": "1",  
"letter": "a"  
}  
}

How can I access the value "one", and how can I take the whole second object into another json field? No string manipulations.

Also, is it possible to pretty print it?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [October 15, 2017, 7:49pm UTC](https://discuss.elastic.co/t/access-json-fields-from-string/103538/2 "2017-10-15T19:49:34Z")

</div>

Use a json filter to parse the JSON string in the `payload` field into discrete Logstash fields. If you want to turn the resulting event back into JSON I think the json\_encode filter will be helpful.

> Also, is it possible to pretty print it?

Pretty print where?

---

<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:** [November 12, 2017, 7:49pm UTC](https://discuss.elastic.co/t/access-json-fields-from-string/103538/3 "2017-11-12T19:49:49Z")

</div>

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