Splunk Search

How do I get arbitrary nested key-value pairs from JSON?

mldavis195
Explorer

I have some JSON that looks similar to this:

 

 

{
    "foo": "bar",
    "x": {
        "hello": "world",
        "y": {
            "A": 400,
            "B": 500,
            "C": 300
        }
    }
}
{
    "foo": "baz",
    "x": {
        "something": "test",
        "y": {
            "A": 100,
            "D": 200,
            "E": 600
        }
    }
}

 

 

 What I would like is to extract everything in x.y for a sum but the keys are dynamic and I won't know them all in advance:

A 500
B 500
C 300
D 200
E 600

 

I have been stuck on this one for a while. Can anyone help me?

Labels (1)
0 Karma
1 Solution

yuanliu
SplunkTrust
SplunkTrust

If that's your raw event, you would have fields like x.y.A, x.y.B, etc., already.  Just do

| stats sum(x.y.*) as *

If they are in an extracted field, say jsonfield, spath first.

| spath input=jsonfield
| stats sum(x.y.*) as *

View solution in original post

yuanliu
SplunkTrust
SplunkTrust

If that's your raw event, you would have fields like x.y.A, x.y.B, etc., already.  Just do

| stats sum(x.y.*) as *

If they are in an extracted field, say jsonfield, spath first.

| spath input=jsonfield
| stats sum(x.y.*) as *

mldavis195
Explorer

Thanks, seems so obvious after seeing your solution.

0 Karma
Get Updates on the Splunk Community!

Technical Workshop Series: Splunk Data Management and SPL2 | Register here!

Hey, Splunk Community! Ready to take your data management skills to the next level? Join us for a 3-part ...

Spotting Financial Fraud in the Haystack: A Guide to Behavioral Analytics with Splunk

In today's digital financial ecosystem, security teams face an unprecedented challenge. The sheer volume of ...

Solve Problems Faster with New, Smarter AI and Integrations in Splunk Observability

Solve Problems Faster with New, Smarter AI and Integrations in Splunk Observability As businesses scale ...