Splunk Search

How do I extract key value pairs from json file using regex?

anooshac
Communicator

Hi all, I have a  sample json file like this.

 

 

{
"Project Name" : "abc",
"Project Group":"A",
"Unit":"B",
"groups_data":[{
"a":"32.064453125",
"b":"5.451171875",
"c":"0.3349609375",
"d":"0.181640625",
"e":"4.58203125",
"f":"81.1611328125"}]
}

 

 

I want to plot a pie chart for the key value pairs present in the groups_data. I tried extracting the data using this query.

 

 

myindex sourcetype="_json"| rex field=_raw "\"group_data\":\[\{\"(?<component>[^/]*)\":"\"(?<Value>\d+)\"\}\]| eval tmp = mvzip(component,Value) |mvexpand tmp |eval component=mvindex(split(tmp,","),0) |eval Value=mvindex(split(tmp,","),1)|chart values(Value) by component

 

 

I am not able to pie chart. It says tmp does not exist.Can anyone tell me is there anything wrong in the regex part? Something i missed anywhere?

Labels (4)
Tags (3)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Extract the pairs as tmp and split using the colon then trim the double-quotes

myindex sourcetype="_json"
| spath output=groups_data groups_data{}
| rex field=groups_data max_match=0 "(?<tmp>\"[^\"]+\":\"[\d\.]+\")"
| mvexpand tmp 
| eval component=trim(mvindex(split(tmp,":"),0),"\"")
| eval Value=trim(mvindex(split(tmp,":"),1),"\"")
| chart values(Value) by component

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust

Try extracting the groups_data (group_data?) with spath and then use max_match=0 for multiple extracts. Also, use a match string that matches your (example) data

myindex sourcetype="_json"
| spath output=groups_data groups_data{}
| rex field=groups_data max_match=0 "\"(?<component>[^\"]+)\":\"(?<Value>[\d\.]+)\""
| eval tmp = mvzip(component,Value) 
| mvexpand tmp 
| eval component=mvindex(split(tmp,","),0) 
| eval Value=mvindex(split(tmp,","),1)
| chart values(Value) by component
0 Karma

anooshac
Communicator

Thank you so much it is working.. Is it mandatory to use spath while using regex?

Also some of the keys have comma because of key is splitting, how can i avoid this? How to escape comma present in key?

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

No, spath is not mandatory for use with rex, you could have extracted the groups_data in to a separate field with rex, then used another rex to extract the components and value pairs - the key here is the max_match=0 to apply the extract pattern multiple times.

Can you give an example of where this is failing (due to commas)?

0 Karma

anooshac
Communicator

Okay.. Thanks alot for the information.

There are some values like below. 

"x,y":"32.064453125

this will be extracted as x and y as key and values. How can i extract "x,y"  as a key?

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Extract the pairs as tmp and split using the colon then trim the double-quotes

myindex sourcetype="_json"
| spath output=groups_data groups_data{}
| rex field=groups_data max_match=0 "(?<tmp>\"[^\"]+\":\"[\d\.]+\")"
| mvexpand tmp 
| eval component=trim(mvindex(split(tmp,":"),0),"\"")
| eval Value=trim(mvindex(split(tmp,":"),1),"\"")
| chart values(Value) by component

jotne
Builder

You could also extract component Value using regex.

| eval component=trim(mvindex(split(tmp,":"),0),"\"")
| eval Value=trim(mvindex(split(tmp,":"),1),"\"")

=

| rex field=tmp "\"(?<component>[^\"]+)\":\"(?<Value>[^\"]+)\""

 

Tags (1)
0 Karma

anooshac
Communicator

Thank you so much for the help! It is working fine now..

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!

Mastering Threat Intelligence in ES 8.5, Splunk AI Assistant v2, and More from Splunk ...

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...

Break the Build: Inside the KubeDoom Lounge at .conf26

    You step up to the machine. The pixelated corridors of a certain 1993 FPS load in front of you, EMP Pulse ...

Splunk Auto Ingestion Parallel Pipeline Scaling

Why this feature matters Many Splunk environments experience ingestion pressure long before the host is fully ...