Knowledge Management

How to extract key value pairs

ts00011
New Member

input: {author=John, book=Splunk }

output table

author book
John Splunk

Tags (1)
0 Karma

niketn
Legend

@ts00011 while posting code/data on Splunk Answers make sure to use the code button 101010 or shortcut Ctrl+K. This will ensure that special characters will not get escaped. If you have a valid JSON data, you should be able to use either one of KV_MODE=json or INDEXED_EXTRACTIONS=json turned on for Splunk to do either Search Time Field Extraction or Index Time Field Extraction (but not both, which will create duplicate result at search time), depending on your use case.

For your example a valid JSON should look like the following (notice colon : and double quotes "😞

{"author":"John","book":"Splunk"}

Following is a run anywhere example to show the output of how JSON data field extraction will look like.

| makeresults
| eval jsonData="{\"author\":\"John\",\"book\":\"Splunk\"}"
| rename jsonData as _raw
| spath

If your data is indeed not valid json and is of the same form as your example, you should still be able to define field extractions (search time) to extract them.

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

jotne
Builder

No need for the renaming to _raw

| makeresults
| eval jsonData="{\"author\":\"John\",\"book\":\"Splunk\"}"
| spath input=jsonData
0 Karma

dmarling
Builder

This will extract that information from _raw for any comma seperated key value pairing, which Splunk will do normally without much prompting, but this format is an odd format since it's wrapped in curly brackets like json, but contains a comma seperated key value pair instead of what I would expect from a json string.

| makeresults count=1
| eval _raw="{author=John, book=Splunk }"
| extract kvdelim="=" pairdelim=","

If your goal is to not have the curly bracket get picked up you can just remove it prior to the extract with this:

| makeresults count=1
| eval _raw="{author=John, book=Splunk }"
| rex field=_raw mode=sed "s/[\{\}]+//g"
| eval _raw=trim(_raw)
| extract kvdelim="=" pairdelim=","
If this comment/answer was helpful, please up vote it. Thank you.
0 Karma

martin_mueller
SplunkTrust
SplunkTrust

Does automatic key=value extraction not work?

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...