Splunk Search

extract all the key-value pairs from the json data

darrfang
Engager

Hi splunk team, 

I have a question about how to extract the key-value pair from json data. Let's say for example I have two raw data like this:

 

# raw data1:
{
  "key1": {
    "key2": {
      "key3": [
        {"data_value": {"aaa": "12345", "bbb": "23456"}}
      ]
    }
  }
}

# raw data 2:
{
  "key1": {
    "key2": {
      "key3": [
        {"data_value": {"ccc": "34567"}}
      ]
    }
  }
}

 

 

how can I extract the key-value results in all the data_value, to be a table as:

 

node    value
aaa     12345
bbb     23456
ccc     34567

 

 

 

I current have a splunk query that could do part of it:

 

```some search...```
| spath output=pairs path=key1.key2.key3{}.data_value
| rex field=hwids "\"(?<node>[^\"]+)\":\"(?<value>[^\"]+)\""
| table node value pairs

 

but this only gives me the result of all the first data, result would look like below, that ignore the data of  "bbb":"23456". Please give me some advice on how to grab all the results, thanks!

 

node    value    pairs
aaa     12345    {"aaa": "12345", "bbb": "23456"}
ccc     34567    {"ccc": "34567"}

 

 

 

Labels (2)
0 Karma
1 Solution

livehybrid
SplunkTrust
SplunkTrust

Hey @darrfang 

How about this?

 

| makeresults 
|  eval _raw="{
  \"key1\": {
    \"key2\": {
      \"key3\": [
        {\"data_value\": {\"aaa\": \"12345\", \"bbb\": \"23456\"}}
      ]
    }
  }
}"
| spath input=_raw output=data_value path=key1.key2.key3{}.data_value
| mvexpand data_value
| eval key_value=split(replace(data_value, "[\{\}\"]", ""), ",")
| mvexpand key_value
| rex field=key_value "\s?(?<node>[^:]+):(?<value>.*)"
| table node value

 

livehybrid_0-1739305566560.png

Please let me know how you get on and consider accepting this answer or adding karma this answer if it has helped.
Regards

Will

View solution in original post

livehybrid
SplunkTrust
SplunkTrust

Hey @darrfang 

How about this?

 

| makeresults 
|  eval _raw="{
  \"key1\": {
    \"key2\": {
      \"key3\": [
        {\"data_value\": {\"aaa\": \"12345\", \"bbb\": \"23456\"}}
      ]
    }
  }
}"
| spath input=_raw output=data_value path=key1.key2.key3{}.data_value
| mvexpand data_value
| eval key_value=split(replace(data_value, "[\{\}\"]", ""), ",")
| mvexpand key_value
| rex field=key_value "\s?(?<node>[^:]+):(?<value>.*)"
| table node value

 

livehybrid_0-1739305566560.png

Please let me know how you get on and consider accepting this answer or adding karma this answer if it has helped.
Regards

Will

darrfang
Engager

Hi @livehybrid

this works like magic! thanks a lot for giving  me the insights!

Just wondering what's the reason at here that you did mvexpand twice

just did some test seems that  if I remove `| mvexpand data_value` I can still get the same results / format

0 Karma

livehybrid
SplunkTrust
SplunkTrust

@darrfang When I tried I think it listed the two pairs within the same row in the table, whereas the second mvexpand broke them into their own rows.

I guess it depends what you're going to do with the data but if you wanted to sort or filter you might want them expanded further? 

Either way, Im glad it worked out for you - Thanks for letting me know 🙂

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!

Take Action Automatically on Splunk Alerts with Red Hat Ansible Automation Platform

 Are you ready to revolutionize your IT operations? As digital transformation accelerates, the demand for ...

Calling All Security Pros: Ready to Race Through Boston?

Hey Splunkers, .conf25 is heading to Boston and we’re kicking things off with something bold, competitive, and ...

Beyond Detection: How Splunk and Cisco Integrated Security Platforms Transform ...

Financial services organizations face an impossible equation: maintain 99.9% uptime for mission-critical ...