Splunk Search

How to extract field names from an object or nested JSON field?

SwervyMcBourbon
Engager

For these following two events:

 

 

{
  "people": {
    "bob": 172,
    "maria": 161
  }
}

{
  "people": {
    "bob": 172,
    "garth": 180
  }
}

 

 

 
I want to report the number of occurrences of each person's name in the "people" object (the field name itself, not its value).  The desired result of my query against the two events above would be:

 

 

bob: 2
maria: 1
garth: 1

 

 

 

What SPL syntax/commands could I use to achieve this?  I have tried a variety of things online to no avail.

 

 

Labels (5)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

See if this helps.

<<your search for people events>>
| spath
```Loop over the people object and make a list of each name found```
| foreach people.* [ eval name=mvappend(name,if(isnotnull('<<FIELD>>'),"<<MATCHSTR>>", null()))]
```Break the name list into separate events```
| mvexpand name
```Count the names```
| stats count by name
---
If this reply helps you, Karma would be appreciated.

View solution in original post

SwervyMcBourbon
Engager

This worked like a charm, thank you @richgalloway !

richgalloway
SplunkTrust
SplunkTrust

See if this helps.

<<your search for people events>>
| spath
```Loop over the people object and make a list of each name found```
| foreach people.* [ eval name=mvappend(name,if(isnotnull('<<FIELD>>'),"<<MATCHSTR>>", null()))]
```Break the name list into separate events```
| mvexpand name
```Count the names```
| stats count by name
---
If this reply helps you, Karma would be appreciated.
Get Updates on the Splunk Community!

Unleash the Power of Splunk MCP and AI, Meet Us at .Conf 2025, and Find Even More New ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Observability Professionals: Build Resilience and Visibility with These .conf25 ...

  If you're focused on performance, availability, and full-stack visibility, the Observability track at ...

See just what you’ve been missing | Observability tracks at Splunk University

Looking to sharpen your observability skills so you can better understand how to collect and analyze data from ...