Splunk Search

Parse JSON only at 1st level

nagar57
Communicator

I want my nested JSON to be parsed only at 1st level instead of parsing all the nested parts. I have below JSON:

 

{ 
"Name":Naman, 
"Age":25, 
"Address":
{ "H.No":"23", 
"Street_no":2, 
"Area":"Model Town" },
 "Country":"IND" 
}

 

I want output like below:

 

Name | Age | Address            | Country
Naman  25   "H.No":"23",          IND
            "Street_no":2,
            "Area":"Model Town"

 

 I don't want to handle Address field separately as these are dynamic fields that are coming in from source.

Labels (1)
0 Karma

yeahnah
Motivator

Hi @nagar57 

You can use spath path=Address to extract the Address values.  Here's a run anywhere example using your source example data ...

 

| makeresults    `comment("# generate dummy data ")`
| eval _raw="{ 
 \"Name\": \"Naman\", 
 \"Age\":25, 
 \"Address\": 
   {\"H.No\":\"23\", 
    \"Street_no\":2, 
    \"Area\":\"Model Town\"},
 \"Country\":\"IND\" 
}"
| spath input=_raw

 `comment("# use the following to extract Address; you should not need to specify input=_raw")`
| spath input=_raw path=Address
| rex mode=sed field=Address "s/[\}\{ ]//g"
| fields Name Age Address Country
| stats values(*) AS * BY Name
| table Name Age Address Country

 

Please mark the post as solved if this helps.

Note: you could also swap the rex command with this eval statement, if you prefer...

| eval Address=replace(Address, "[\}\{ ]", "")

 

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!

SOC4Kafka - New Kafka Connector Powered by OpenTelemetry

The new SOC4Kafka connector, built on OpenTelemetry, enables the collection of Kafka messages and forwards ...

Rounding off the Splunk Dashboard Contest

What does a contest-winning Splunk dashboard look like? In this case, it isn't in a browser tab at all. It ...

A Four Part Event Series: AI + Observability: AI Agents, LLMs, Apps, & Infrastructure

AI + Observability: AI Agents, LLMs, Apps, & Infrastructure The rapid evolution of artificial intelligence ...