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!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer Certification at ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...