Splunk Search

Grouping and counting items based on values in the list?

ghostrider
Path Finder

I have below JSON event where there are errors present in a field which is a list. I want to extract the values in the list and group them with another field which is part of an object of the same event.  After grouping I want to count them like below output. I am using below query but not getting the expected output. Any help on this will be highly appreciated. 

Sample JSON Event1

 

{
"errorList": ["There is an ErrorA", "There is some other ErrorB", "Ohh another ErrorC"],
"Details": {
"type": "ABC"
}
}

 

 

Sample JSON Event2

 

{
"errorList": ["There is some other ErrorB", "Ohh another ErrorC"],
"Details": {
"type": "XYZ"
}
}

 

 

Expected Output

 

Type   Error                                Count
ABC    There is some other ErrorB           3
ABC    There is an ErrorA                   4
XYZ    Ohh another ErrorC                   2

 

 

Query I am trying 

 

 

BASE_SEARCH
| rex field=MESSAGE "(?<JSON>\{.*\})"
| spath input=JSON
| rename Details{}.type as "Type"  
| rename errorList{} as "Error"
| stats count as Count by "Type" "Error"
| table Type, Error , Count

 

0 Karma
1 Solution

yuanliu
SplunkTrust
SplunkTrust

Your data doesn't have a field named Details{}.type because Details is not an array.  Use Details.type will solve your problem.

 

BASE_SEARCH
| rex field=MESSAGE "(?<JSON>\{.*\})"
| spath input=JSON
| rename Details.type as "Type"  
| rename errorList{} as "Error"
| stats count as Count by "Type" "Error"
| table Type, Error , Count

 

With the sample JSON events you give, the output is

TypeErrorCount
ABCOhh another ErrorC1
ABCThere is an ErrorA1
ABCThere is some other ErrorB1
XYZOhh another ErrorC1
XYZThere is some other ErrorB1

The output table you listed is not from those two datapoints.  If you want to tally by Error alone, the search should be

 

BASE_SEARCH
| rex field=MESSAGE "(?<JSON>\{.*\})"
| spath input=JSON
| rename Details.type as "Type"  
| rename errorList{} as "Error"
| stats values(Type) as Type count as Count by "Error"
| table Type, Error , Count

 

With this, you'll get something like

Type
ErrorCount
ABC
XYZ
Ohh another ErrorC2
ABCThere is an ErrorA1
ABC
XYZ
There is some other ErrorB2

View solution in original post

yuanliu
SplunkTrust
SplunkTrust

Your data doesn't have a field named Details{}.type because Details is not an array.  Use Details.type will solve your problem.

 

BASE_SEARCH
| rex field=MESSAGE "(?<JSON>\{.*\})"
| spath input=JSON
| rename Details.type as "Type"  
| rename errorList{} as "Error"
| stats count as Count by "Type" "Error"
| table Type, Error , Count

 

With the sample JSON events you give, the output is

TypeErrorCount
ABCOhh another ErrorC1
ABCThere is an ErrorA1
ABCThere is some other ErrorB1
XYZOhh another ErrorC1
XYZThere is some other ErrorB1

The output table you listed is not from those two datapoints.  If you want to tally by Error alone, the search should be

 

BASE_SEARCH
| rex field=MESSAGE "(?<JSON>\{.*\})"
| spath input=JSON
| rename Details.type as "Type"  
| rename errorList{} as "Error"
| stats values(Type) as Type count as Count by "Error"
| table Type, Error , Count

 

With this, you'll get something like

Type
ErrorCount
ABC
XYZ
Ohh another ErrorC2
ABCThere is an ErrorA1
ABC
XYZ
There is some other ErrorB2

johnhuang
Motivator

You just need to expand the multivalue field into its own events.

BASE_SEARCH
| rex field=MESSAGE "(?<JSON>\{.*\})"
| spath input=JSON
| rename Details{}.type as "Type"
| rename errorList{} as "Error"
| mvexpand Error
| stats count as Count by "Type" "Error"
| table Type, Error , Count

ghostrider
Path Finder

Thanks!!! I ran the above query but it shows no result.  Do I need to change the order of mvexpand or need to add some other method altogether?

0 Karma
Get Updates on the Splunk Community!

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...