Json Parsing

i have event coming in my logstash as a json

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

I wanted too put that event inside a key data like

{"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

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

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 that works on all filters.

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

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

{"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

Can you show what your event looks like in

output { stdout { codec => rubydebug } }

and also what you want it to look like.

Does encasing it in single quotes work ?

Hello,

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

the new filter to resolve your problem :

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

your output the same (in your post)

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

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