Splunk Enterprise

Correlation of 2 fields within json array

galsegal
Explorer

Hey All,

 

What I'm trying to do is to build a search query that correlates between fields like in the below example:

galsegal_0-1595233816225.png

I need that where message.anomaly.features{}.anomaly has a true value, then to output a new field with the corresponding fields below - 23, location (Even only one of them is good for me)

 

How can I accomplish that?

 

Thank you,

Labels (1)
0 Karma
1 Solution

anmolpatel
Builder

@galsegal Is this what you're after ?

| makeresults
| eval _raw="{
    \"Features\": [
        { 
            \"anomaly\": false,
            \"id\" : 25,
            \"name\" : \"service\"
        },
        { 
            \"anomaly\": true,
            \"id\" : 23,
            \"name\" : \"location\"
        },
        { 
            \"anomaly\": false,
            \"id\" : 24,
            \"name\" : \"ip\"
        },
        { 
            \"anomaly\": false,
            \"id\" : 27,
            \"name\" : \"time\"
        }
    ]
}"
| rename COMMENT AS "The code below is what is needed. First extract each value from the tree, than we group and split them based on how they are related." 
| spath path="Features{}.anomaly" output=anomaly
| spath path="Features{}.id" output=id
| spath path="Features{}.name" output=name
| eval x = mvzip(mvzip(id, anomaly, "\n"), name, "\n")
| mvexpand x
| eval x=split(x,"\n")
| eval ID = mvindex(x, 0)
| eval Name = mvindex(x, 1)
| eval Anomaly = mvindex(x, 2)
| stats values(Name) as Name values(Anomaly) as Anomaly by ID

View solution in original post

0 Karma

anmolpatel
Builder

@galsegal Is this what you're after ?

| makeresults
| eval _raw="{
    \"Features\": [
        { 
            \"anomaly\": false,
            \"id\" : 25,
            \"name\" : \"service\"
        },
        { 
            \"anomaly\": true,
            \"id\" : 23,
            \"name\" : \"location\"
        },
        { 
            \"anomaly\": false,
            \"id\" : 24,
            \"name\" : \"ip\"
        },
        { 
            \"anomaly\": false,
            \"id\" : 27,
            \"name\" : \"time\"
        }
    ]
}"
| rename COMMENT AS "The code below is what is needed. First extract each value from the tree, than we group and split them based on how they are related." 
| spath path="Features{}.anomaly" output=anomaly
| spath path="Features{}.id" output=id
| spath path="Features{}.name" output=name
| eval x = mvzip(mvzip(id, anomaly, "\n"), name, "\n")
| mvexpand x
| eval x=split(x,"\n")
| eval ID = mvindex(x, 0)
| eval Name = mvindex(x, 1)
| eval Anomaly = mvindex(x, 2)
| stats values(Name) as Name values(Anomaly) as Anomaly by ID
0 Karma

galsegal
Explorer

This was not 100% the solution but it indeed got me there 🙂

Thank you very much, sir.

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!

Best Practices: Splunk auto adjust pipeline queue

When you enable autoAdjustQueue in Splunk, maxSize should be understood as the queue size Splunk starts with ...

Laser Bananas and Edge Hubs: Exploring Operational Technology (OT) Data Through a ...

  OT is a different environment to traditional IT and can have interesting challenges when interfacing the ...

Event Series: Mastering AI Tokenomics and Splunk Agent Observability

Beyond the Black Box: Correlating AI Performance and Tokenomics with Splunk Agent Observability   As ...