Splunk Search

Extract field value from json string with different spath and group by

newbie77
Engager
I have splunk events that has a splunk field as json string named "data"
I want to group these events by "id". This id could appear in 2 different path.
 
 
Event Type 1
data= {
      "response": {
       "resources":[
            {
               "type": "loginUser",
               "id": "1234"
           }
        ]
 }
}
 
Event Type 2
data= {
       "response": {
               "actors":
               {
                      "type": "loginUser",
                     "id": "1234"
              }
        }
}

 

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Assuming you have already extracted the data field, and that the string in data is valid JSON (which you example is not), you could try this

| spath input=data
| where 'response.action.type'="UserCreated" OR 'response.action.type'="TxCreated"
| eval id = coalesce('response.resources{}.id', 'response.actors.id')

View solution in original post

newbie77
Engager

Many thanks for the help.  I want to expand the requirement as follows:

For  an "id" there could be  upto 12 max possible different events with response.action.type="UserCreated" or response.action.type="TxCreated"  or response.action.type="TxUpdated" and 9 other types.

The goal is to group by "id" where only 2 action types have occured namely:

  •            response.action.type="UserCreated" (Event1) and
  •            response.action.type="TxCreated"  (Event 2)

 

Event Type 1
data= {
      "response": {
               "action": {
                    "type": "UserCreated",
               }
       "resources":[
            {
               "type": "loginUser",
               "id": "1234"
           }
        ]
 }
}
 
Event Type 2
data= {
       "response": {
              "action": {
                   "type": "TxCreated",
               }
               "actors":
               {
                      "type": "loginUser",
                     "id": "1234"
              }
        }
}
 
Event Type 3
data= {
       "response": {
              "action": {
                   "type": "TxUpdated",
               }
               "actors":
               {
                      "type": "loginUser",
                     "id": "1234"
              }
        }
}

 

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Assuming you have already extracted the data field, and that the string in data is valid JSON (which you example is not), you could try this

| spath input=data
| where 'response.action.type'="UserCreated" OR 'response.action.type'="TxCreated"
| eval id = coalesce('response.resources{}.id', 'response.actors.id')

ITWhisperer
SplunkTrust
SplunkTrust
| spath input=data
| foreach *.id
    [| eval id=if(isnotnull('<<FIELD>>'),'<<FIELD>>',id)]
0 Karma

yuanliu
SplunkTrust
SplunkTrust

Alternatively, if you already have all possible paths and there are not too many, coalesce can be more succinct.  For the two path illustrated,

| eval id = coalesce('response.resources.id', 'respones.actors.id')
0 Karma
Get Updates on the Splunk Community!

Index This | What is broken 80% of the time by February?

December 2025 Edition   Hayyy Splunk Education Enthusiasts and the Eternally Curious!    We’re back with this ...

Unlock Faster Time-to-Value on Edge and Ingest Processor with New SPL2 Pipeline ...

Hello Splunk Community,   We're thrilled to share an exciting update that will help you manage your data more ...

Splunk MCP & Agentic AI: Machine Data Without Limits

Discover how the Splunk Model Context Protocol (MCP) Server can revolutionize the way your organization uses ...