# Json Parsing

**URL:** <https://discuss.elastic.co/t/json-parsing/286277>\
**Category:** Logstash\
**Created:** [October 8, 2021, 7:47pm UTC](https://discuss.elastic.co/t/json-parsing/286277 "2021-10-08T19:47:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_snow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/john_snow/32/77761_2.png) [@John\_snow](https://discuss.elastic.co/u/John_snow)\
**Post date:** [October 8, 2021, 7:47pm UTC](https://discuss.elastic.co/t/json-parsing/286277/1 "2021-10-08T19:47:36Z")

</div>

i have event coming in my logstash as a json

```auto
{"key1": "value1", "key2": "value2", "key3": {"key4": "value4"}}

```

I wanted too put that event inside a key data like

```auto
{"data" : {
           "key1": "value1", 
           "key2": "value2", 
           "key3": {"key4": "value4"}
       }
}

```

But not sure how i can do that in logstash.

Also i wanted add new fields in the event as

```auto
 {
     "data" : {
                "key1": "value1", 
                "key2": "value2", 
                "key3": {
                         "key4": "value4"
                        }
              },
     "new_filed": "new_value"
}

```

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [October 8, 2021, 8:51pm UTC](https://discuss.elastic.co/t/json-parsing/286277/2 "2021-10-08T20:51:19Z")

</div>

You could parse the JSON with a json filter and use the target option to put it into a field.

To add new fields you can use the common option [add\_field](https://www.elastic.co/guide/en/logstash/current/plugins-filters-json.html#plugins-filters-json-add_field) that works on all filters.

---

<div class="post-metadata">

**Author:** ![John\_snow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/john_snow/32/77761_2.png) [@John\_snow](https://discuss.elastic.co/u/John_snow)\
**Post date:** [October 11, 2021, 1:18pm UTC](https://discuss.elastic.co/t/json-parsing/286277/3 "2021-10-11T13:18:10Z")

</div>

Thanks for the rely.  
You mean like this?  
json {  
source =\> "key1", "key2", "key3"  
target =\> "data"  
}

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [October 11, 2021, 4:54pm UTC](https://discuss.elastic.co/t/json-parsing/286277/4 "2021-10-11T16:54:00Z")

</div>

The source option should point to a field that contains the JSON string.

---

<div class="post-metadata">

**Author:** ![John\_snow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/john_snow/32/77761_2.png) [@John\_snow](https://discuss.elastic.co/u/John_snow)\
**Post date:** [October 11, 2021, 10:10pm UTC](https://discuss.elastic.co/t/json-parsing/286277/5 "2021-10-11T22:10:38Z")

</div>

```auto
{"key1": "value1", "key2": "value2", "key3": {"key4": "value4"}}

```

in my case i'm getting event as follow and i wanted to put whole inside the data field

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [October 11, 2021, 11:01pm UTC](https://discuss.elastic.co/t/json-parsing/286277/6 "2021-10-11T23:01:34Z")

</div>

Can you show what your event looks like in

```
output { stdout { codec => rubydebug } }

```

and also what you want it to look like.

---

<div class="post-metadata">

**Author:** ![zx8086](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zx8086/32/94917_2.png) [@zx8086](https://discuss.elastic.co/u/zx8086)\
**Post date:** [October 11, 2021, 11:32pm UTC](https://discuss.elastic.co/t/json-parsing/286277/7 "2021-10-11T23:32:02Z")

</div>

> [@John\_snow](#):
>
> `'{"key1": "value1", "key2": "value2", "key3": {"key4": "value4"}}'`

Does encasing it in single quotes work ?

---

<div class="post-metadata">

**Author:** ![Mustafa\_NAJIB](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mustafa_najib/32/95797_2.png) [@Mustafa\_NAJIB](https://discuss.elastic.co/u/Mustafa_NAJIB)\
**Post date:** [October 13, 2021, 9:21am UTC](https://discuss.elastic.co/t/json-parsing/286277/8 "2021-10-13T09:21:29Z")

</div>

Hello,

This is your input data :  
{"key1": "value1", "key2": "value2", "key3": {"key4": "value4"}}

the new filter to resolve your problem :

```auto
filter {
		json {
			source => "message"
		}
		
		mutate {			
			add_field => { "new_filed" => "new_value" }
		}
}

```

your output the same (in your post)

```auto
{
  "key1": "value1",
  "key2": "value2",
  "key3": {
    "key4": "value4"
  },
  "new_filed": "new_value"
}

```

---

<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 10, 2021, 9:22am UTC](https://discuss.elastic.co/t/json-parsing/286277/9 "2021-11-10T09:22:18Z")

</div>

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