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
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!

Network to App: Observability Unlocked [May & June Series]

In today’s digital landscape, your environment is no longer confined to the data center. It spans complex ...

SPL2 Deep Dives, AppDynamics Integrations, SAML Made Simple and Much More on Splunk ...

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

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...