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!

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...

Index This | Forward, I’m heavy; backward, I’m not. What am I?

April 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...