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!

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...