Splunk Search

Having trouble reading complexed nested Json

chrisboy68
Contributor

Hi, struggling trying to count objects in a big json doc. I'm on version 8.0.5, so function json_keys is not available. 

 

 

 

{
	"0": {
		"field1": "123"
	},
	"1": {
		"field2": "123"
	},
	"2": {
		"field3": "123"
	},
	"3": {
		"field4": "123"
	},
	"4": {
		"field5": "123"
	}


}

 

 

 

This is a sample, I am able to get down to the path (startpath) with spath. What I'm trying to do is count the instances of the objects (0,1,2,3,4). I can't cleanly regex backwards as the real values names are not consistent.   Thought I could do something like startpath{} and list them out , but the wildcards {} are not working anyway I try it. Thoughts, suggestions?

 

Thanks

 

Chris

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust
| makeresults 
| eval _raw="{
	\"0\": {
		\"field1\": \"123\"
	},
	\"1\": {
		\"field2\": \"123\"
	},
	\"2\": {
		\"field3\": \"123\"
	},
	\"3\": {
		\"field4\": \"123\"
	},
	\"4\": {
		\"field5\": \"123\"
	}


}"
| spath
| foreach *.*
    [| eval startpath_count=if(isnull(startpath_count),mvcount('<<FIELD>>'),startpath_count+mvcount('<<FIELD>>'))]
| stats sum(startpath_count) as startpath_count

View solution in original post

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Will this work for you?

| makeresults 
| eval _raw="{\"startpath\": {
    \"0\": {
        \"ID\": \"123\"
     },
     \"1\": {
        \"ID\": \"123\"
     },
     \"2\": {
        \"ID\": \"123\"
     },
     \"3\": {
        \"ID\": \"123\"
     },
     \"4\": {
        \"ID\": \"123\"
     },
     \"4\": {
      \"ID\": \"123\"
   }
}
}"
| spath
| foreach startpath.*.ID
    [| eval startpath_<<MATCHSEG1>>_count=mvcount('<<FIELD>>')]
| fields - startpath.*.*
0 Karma

chrisboy68
Contributor

sorry, i made the sample too easy. I updated my sample json. No, I need to count the instance of the object. 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Simplifying doesn't always help - in this instance, your example is not valid JSON format. Please can you update the example with a valid, and possibly more representative example?

0 Karma

chrisboy68
Contributor

Ok I updated. Just know that the sample is deeply nested and I can get to this object starting with an initial spath.

Tags (1)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| makeresults 
| eval _raw="{
	\"0\": {
		\"field1\": \"123\"
	},
	\"1\": {
		\"field2\": \"123\"
	},
	\"2\": {
		\"field3\": \"123\"
	},
	\"3\": {
		\"field4\": \"123\"
	},
	\"4\": {
		\"field5\": \"123\"
	}


}"
| spath
| foreach *.*
    [| eval startpath_<<MATCHSEG1>>_count=mvcount('<<FIELD>>')]
| stats sum(startpath_*_count) as startpath_*_count
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| makeresults 
| eval _raw="{
	\"0\": {
		\"field1\": \"123\"
	},
	\"1\": {
		\"field2\": \"123\"
	},
	\"2\": {
		\"field3\": \"123\"
	},
	\"3\": {
		\"field4\": \"123\"
	},
	\"4\": {
		\"field5\": \"123\"
	}


}"
| spath
| foreach *.*
    [| eval startpath_count=if(isnull(startpath_count),mvcount('<<FIELD>>'),startpath_count+mvcount('<<FIELD>>'))]
| stats sum(startpath_count) as startpath_count
0 Karma

chrisboy68
Contributor

Neat. Trying to follow. I need to have the total of all, not each count. So, in my example, the total is 5.

0 Karma

chrisboy68
Contributor

I'm almost there. Now I need to count by each event, as this is totaling for every single event.  Looks like I just need to add a group by in the stats. Thank you!

0 Karma
Get Updates on the Splunk Community!

Introducing the Splunk Community Dashboard Challenge!

Welcome to Splunk Community Dashboard Challenge! This is your chance to showcase your skills in creating ...

Get the T-shirt to Prove You Survived Splunk University Bootcamp

As if Splunk University, in Las Vegas, in-person, with three days of bootcamps and labs weren’t enough, now ...

Wondering How to Build Resiliency in the Cloud?

IT leaders are choosing Splunk Cloud as an ideal cloud transformation platform to drive business resilience,  ...