Splunk Search

How to get filtered data with spath?

developer_de
New Member

I am trying to build stats for a set of JSON data which looks like this:

{
   "ts":1527498793267,
   "version":"1.12.7",
   "data":{
      "myList":[
         {
            "id":"180528_090203",
            "nativeRequestReceived":1,
            "nativeResponseSent":1
         },
         {
            "id":"180528_090129",
            "nativeRequestReceived":1,
            "nativeResponseSent":1
         }
      ],
      "freemem":6534152192
   },
   "time":"2018-05-28T09:13:13.267Z",
   "v":0
}

I am using following splunk command to build stats table:

spath path=data.myList{} output=myList | spath input=myList | stats sum(nativeRequestReceived) sum(nativeResponseSent) by id

I use sum here because there will be multiple JSON objects like the one written above and I would like to add all nativeRequestReceived and nativeResponseSent for each id and group them by id. It can be seen clearly that there are 2 different ids each with 1 nativeRequestReceived. But the output looks like this. It ignores one of the ids. Can someone help me in clarifying what went wrong here?

id                sum(nativeRequestReceived)     sum(nativeResponseSent)
180528_090203           1                             1

Thanks

0 Karma

ansif
Motivator

Hi,

Have you tried below answer? Accept if works.

Thanks

0 Karma

ansif
Motivator
| makeresults |eval _raw="{
    \"ts\":1527498793267,
    \"version\":\"1.12.7\",
    \"data\":{
       \"myList\":[
          {
             \"id\":\"180528_090203\",
             \"nativeRequestReceived\":1,
             \"nativeResponseSent\":1
          },
          {
             \"id\":\"180528_090129\",
             \"nativeRequestReceived\":1,
             \"nativeResponseSent\":1
          }
       ],
       \"freemem\":6534152192
    },
    \"time\":\"2018-05-28T09:13:13.267Z\",
    \"v\":0
 }"|spath output=id path=data.myList{}.id
| spath output=nativeRequestReceived path=data.myList{}.nativeRequestReceived
| spath output=nativeResponseSent path=data.myList{}.nativeResponseSent| stats sum(nativeRequestReceived) sum(nativeResponseSent) by id
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 ...