Splunk Search

How to extract json values

tsm0099
Explorer

I have an event in json which has key pairs like:

{

"timestamp": 157281937,

"message":"abc\xyz\pqr\efg",

}

 

I have to create a table of message with with four columns and the columns in the table will have the values abc,xyz,qpr,efg...

 

How can i do this?

 

Thanks in advance!

 

Labels (1)
0 Karma

to4kawa
Ultra Champion
index=_internal | head 1 | fields _raw |eval _raw="{
\"timestamp\": 157281937,
\"message\":\"abc\xyz\pqr\efg\",
}"
| rex mode=sed "s/,\s+}/}/g s/\\\/#/g"
| spath message
| rex field=message "(?<col1>\w+)#(?<col2>\w+)#(?<col3>\w+)#(?<col4>\w+)"

Why don't you change the characters?

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

 

| spath message
| rex field=message "(?<col1>[^\\\]+)\\\(?<col2>[^\\\]+)\\\(?<col3>[^\\\]+)\\\(?<col4>.+)"
| table col1 col2 col3 col4

 

0 Karma

tsm0099
Explorer

Throws an error...

Regex: missing terminating for character class.

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Try your data and the rex expression at regex101.com to see where the error is coming from.

0 Karma

tsm0099
Explorer

It works fine in regex101...

But when i use it in splunk search it throws the error:

Encountered the following error while compiling the regex '(?<col1>[^\]+)\(?<col2>[^\]+)\(?<col3>[^\]+)\(?<col4>.+)'

 

Note: in the error regex i only see single \ instead of \\

 

Do you have any idea about thia?

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

You need \\ in regex and \\\ in splunk

ITWhisperer_0-1603793912587.png

 

| makeresults | eval _raw="abc\\xyz\\pqr\\efg"
| rex "(?<col1>[^\\\]+)\\\(?<col2>[^\\\]+)\\\(?<col3>[^\\\]+)\\\(?<col4>.+)"

ITWhisperer_0-1603794333524.png

 

 

Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...