All Apps and Add-ons

Unable to extract all keys.

sagar1905
New Member

I've a log to extract key value pairs from but I'm unable to extract all of them

Sample log:
2017-12-19 19:33:55,848 LL="DEBUG" CR="abq_asdaasd_adasdq_DAS" RE="abd" DE="abc" TR="tomcat-http--41" LN="xyz" TYPE="IN-RESPONSE" BODY="{"key":{"key1":"value1","key2":"value2","dateAndTimeTransmission":"2017-12-19T19:33:54.000Z","key3":"value3","key4":"value4","key5":"value5","key6":"value6","key":2,"key":{"key7":"value7","key8":"value8","key9":"value9"},"key":{"key":[{"key10":"value10","key11":"value12","key13":"value13"},{"key14":"value14","key":"value","key":"value"}]},"key":"value","key":"value"}}"

I'm using the query ----- > index=xyz ( $searchKey$ ) | extract pairdelim="," kvdelim=":" | dedup key ------ then followed by putting the value in the table.

I require key10,value10 and key11,value11 I'm able to extract key11,value11 but my query is not extracting key10,value10 .. maybe due to the [{

Can someone please help?

0 Karma

niketn
Legend

@sagar1905, you should be using spath command to parse and extract JSON data. Try the following run anywhere search based on your data. As per your data BODY should show up during field discovery with the complete JSON data. If not you might have to create a regular expression to extract complete JSON data to a field first using rex command.

| makeresults
| eval BODY="{\"key\":{\"key1\":\"value1\",\"key2\":\"value2\",\"dateAndTimeTransmission\":\"2017-12-19T19:33:54.000Z\",\"key3\":\"value3\",\"key4\":\"value4\",\"key5\":\"value5\",\"key6\":\"value6\",\"key\":2,\"key\":{\"key7\":\"value7\",\"key8\":\"value8\",\"key9\":\"value9\"},\"key\":{\"key\":[{\"key10\":\"value10\",\"key11\":\"value12\",\"key13\":\"value13\"},{\"key14\":\"value14\",\"key\":\"value\",\"key\":\"value\"}]},\"key\":\"value\",\"key\":\"value\"}}"
| spath input=BODY

Refer to spath documentation for details: http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Spath

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

sagar1905
New Member

The keys are fixed but the values aren't, will the above query still work? And the keys also get changed sometimes, is there a general way to extract this?

0 Karma

niketn
Legend

@sagar1905, yes it should work. Spath command is used to traverse XML and JSON to extract Key Value pairs. Do check the command reference. Instead of first two commands i.e. | makeresults and | eval BODY=... , you can put your Base search to extract the JSON data field i.e. BODY as per your question an then feed the same to spath command to get the extracted fields.

Please try out and confirm!

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...