Splunk Search

How to get specific value from key/value array to form table?

btsr
Explorer

Hi All,

I don't have much experience with Splunk. My JSON payload looks like as shown below. The msg.details array can have any number key/value pairs in any order.

 

 

{
  "appName": "TestApp",
  "eventType": "Response",
  "msg": {
    "transId": "Trans1234",
    "status": "Success",
    "client": "clientXyz",
    "responseTime": 1650,
    "details": [
      {
        "keyName": "rtt",
        "keyValue": 2778
      },
      {
        "keyName": "trace",
        "keyValue": 97007839130680
      }
    ],
    "url": "/v1/test"
  }
}

 

 

 I am trying to write a query and form a table as shown below. I am interested in displaying only the keyValue of keyName:trace in the table. Any help is appreciated. Thanks.

index=* appName="TestApp" msg.url="/v1/test" | table msg.transId, msg.status, msg.details[keyName="trace"].keyValue

msg.transId msg.status msg.details[keyName="trace"].keyValue
Trans1234 Success 97007839130680
Trans7890 ERROR 29411645500355

 

Labels (2)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Another way to do it is to extract and expand the collection

| spath
| spath msg.details{} output=details
| mvexpand details
| spath input=details
| where keyName="trace"
| table msg.transId msg.status keyValue

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust

Another way to do it is to extract and expand the collection

| spath
| spath msg.details{} output=details
| mvexpand details
| spath input=details
| where keyName="trace"
| table msg.transId msg.status keyValue

btsr
Explorer

This saved my day! Thank you @ITWhisperer!

Tags (1)

richgalloway
SplunkTrust
SplunkTrust

Splunk doesn't recognize square brackets as denoting a JSON array; they identify a subsearch.

We also can't reference a JSON array element by name.  Perhaps some day...

Here's a run-anywhere query showing one way to accomplish the task.

| makeresults 
| eval _raw="{
  \"appName\": \"TestApp\",
  \"eventType\": \"Response\",
  \"msg\": {
    \"transId\": \"Trans1234\",
    \"status\": \"Success\",
    \"client\": \"clientXyz\",
    \"responseTime\": 1650,
    \"details\": [
      {
        \"keyName\": \"rtt\",
        \"keyValue\": 2778
      },
      {
        \"keyName\": \"trace\",
        \"keyValue\": 97007839130680
      }
    ],
    \"url\": \"/v1/test\"
  }
}" | spath 
``` Everything above sets up test data.  Delete IRL. ```
``` Combine keyName and keyValue so we can work with them as a pair. ```
| eval foo=mvzip('msg.details{}.keyName','msg.details{}.keyValue')
``` Locate "trace" keys ```
| eval foo=mvindex(foo,mvfind(foo,"trace"))
``` Break up the keyName/keyValue pair for display ```
| eval foo=split(foo,",")
| eval keyName=mvindex(foo,0), keyValue=mvindex(foo,1)
| table msg.transId, msg.status, keyName, keyValue
---
If this reply helps you, Karma would be appreciated.
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!

Monitoring AI Agents with Splunk Observability Cloud

Let’s say I’m running a travel planning AI app in production. A user asks for three concise hotel options in ...

[Puzzles] Solve, Learn, Repeat: Tiling

This puzzle (first published here) is based on finding groups of tessellated tiles (inspired by floor tiles I ...

SOK it to Me: Top 3 Benefits of Using Splunk Operator on Kubernetes that’ll Make ...

    Thursday, July 9, 2026  |  11:00AM–12:00PM PDT Duration: 1 hour (includes Q&A) Managing can feel like a ...