Splunk Search

Troubling json extraction

Cuyose
Builder

I can't for the life of me figure this out. There seem to be examples all over and none of them address this.

I have a json element that looks like this.(I reduced the comma delimited elements to 2 for clarity, but there are dozens in the same format.
{"status":"success","data":{"11-01-00251":"88","11-01-00247":"378"}}

Splunk is displaying the event in structured json just fine in the event list. What I want to do is be able to report on this data. This is inventory. So the first data element is the sku, and the integer is the quantity. Basically how do I search to be able to do a

|chart inventory by sku?

Tags (2)
0 Karma

somesoni2
Revered Legend

This seems to work for me.

|stats count | eval _raw="{\"status\":\"success\",\"data\":{\"11-01-00251\":\"88\",\"11-01-00247\":\"378\"}}" | rex max_match=0 "\"(?[^\"]*)\":\"(?\d+)"

0 Karma

aweitzman
Motivator

You're trying to get Splunk to interpret field names as field values, which isn't easy. You might be well served to rewrite the output (either at the source, or possibly via a transform) like so:

{"status":"success","data":[{"sku":"11-01-00251","inventory":"88"},{"sku":"11-01-00247","inventory":"378"}]}

Then you can work with it through spath using something like example 3 on the spath documentation page.

0 Karma

aweitzman
Motivator
  1. You might want to consider a method that does the string manipulation for a single unit of "data", and then using python's "join" command to put the results together the way you want. (I don't really know python, but that seems to be how it ought to work based on what I found on the internet.)

  2. Since you're adding it in as text, you might want to avoid naming the second field "count" since that's also a Splunk command.

0 Karma

Cuyose
Builder

Thats what Im resorting to, I have a python script to do it and it's all done except for the little part where its adding a comma on the last data object invalidating the json.

for sku in data:
print '{"sku":"'+ sku + '","count":"' + str(data[sku]) + '"},'

0 Karma

Cuyose
Builder

Maybe even a regex that can extract this would be useful, Splunk's field extraction can't seem to figure it out.

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!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...