Splunk Search

JSON array and stats command

TSplunk
Engager

Hi,

I am having trouble generating a stats report based on JSON data containing an array.  I want to produce the following report:

ErrorCodeErrorMessageCount
212The image quality is poor1
680The image could not be found1
809Document not detected1
 
 
 
When I do the stats command, I do not get any results:
 
| spath input=jsondata |stats count by "embedded.metadata.data.results{}.notifications.*"
 
I have to know the error code value in the array in order to get any stats output.  For example:
 
| spath input=jsondata |stats count by "embedded.metadata.data.results{}.notifications.809"
 

Result:

embedded.metadata.data.results{}.notifications.809count
Document not detected1

 

Here example of the json data


{
"_embedded": {
  "metadata": {
     "environment": {
         "id": "6b3dc"
      },
"data": {
   "results": [
    {
     "documentId": "f18a20f1",
     "notifications": {
         "212": "The image quality was poor"
       }
   },
{
   "documentId": "f0fdf5e8c",
   "notifications": {
      "680": "The image could not be found"
     }
},
{
   "documentId": "95619532",
   "notifications": {
      "809": "Document not detected"
    }
}
]
}
}
}
}

Thanks in advance for any assistance!!

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust
| spath _embedded.metadata.data.results{}.notifications output=results
| mvexpand results
| rex field=results "\"(?<ErrorCode>\d+)\":\s*\"(?<ErrorMessage>[^\"]*)\""
| stats count by ErrorCode ErrorMessage

View solution in original post

0 Karma

yuanliu
SplunkTrust
SplunkTrust

This is an interesting challenge because of the leading underscore (_) in the root node key.  spath can't seem to recognize the path as is. (Could be a subtle bug.)  One potential way to work around this is to use text replacement to get rid of the underscore. But I prefer a more syntactic method.  In Splunk 9, you can do fronjson with an arbitrary prefix so spath can do its job. (You can also use fromjson repeatedly.  But with deep paths like this problem, that's undesirable.)

 

| fromjson jsondata prefix=my
| spath input=my_embedded path=metadata.data.results{}
| mvexpand metadata.data.results{}
| spath input=metadata.data.results{}
| foreach notifications.*
    [eval ErrorCode = if(isnull(ErrorCode), "<<MATCHSTR>>", ErrorCode), ErrorMessage = mvappend(ErrorMessage, '<<FIELD>>')]
| stats count by ErrorCode ErrorMessage

 

Your sample data results in

ErrorCodeErrorMessagecount
212Document not detected1
212The image could not be found1
212The image quality was poor1

This is an emulation you can play with and compare with real data

 

| makeresults
| eval jsondata = "{
\"_embedded\": {
  \"metadata\": {
     \"environment\": {
         \"id\": \"6b3dc\"
      },
\"data\": {
   \"results\": [
    {
     \"documentId\": \"f18a20f1\",
     \"notifications\": {
         \"212\": \"The image quality was poor\"
       }
   },
{
   \"documentId\": \"f0fdf5e8c\",
   \"notifications\": {
      \"680\": \"The image could not be found\"
     }
},
{
   \"documentId\": \"95619532\",
   \"notifications\": {
      \"809\": \"Document not detected\"
    }
}
]
}
}
}
}"

 

Note: Once you get past that leading underscore in JSON path, you can then use the text manipulation method proposed by @ITWhisperer (with a small path correction), like this

 

| fromjson jsondata prefix=my
| spath input=my_embedded path=metadata.data.results{}
| mvexpand metadata.data.results{} ``` not metadata.data.results{}.notifications ```
| rex field=metadata.data.results{} "\"(?<ErrorCode>\d+)\":\s*\"(?<ErrorMessage>[^\"]*)\""
| stats count by ErrorCode ErrorMessage

 

But text manipulation on structured data is not as robust because a developer/software can always change format in the future without altering semantics.

Tags (1)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| spath _embedded.metadata.data.results{}.notifications output=results
| mvexpand results
| rex field=results "\"(?<ErrorCode>\d+)\":\s*\"(?<ErrorMessage>[^\"]*)\""
| stats count by ErrorCode ErrorMessage
0 Karma
Get Updates on the Splunk Community!

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...

Detecting Remote Code Executions With the Splunk Threat Research Team

REGISTER NOWRemote code execution (RCE) vulnerabilities pose a significant risk to organizations. If ...