Splunk Search

How to extract a key value pair, convert it to a JSON format and assign it to a variable using eval?

nibinabr
Communicator

Hi Guys,

I have a log event like the following

2015-01-08T08:56:30.835Z "Name:John"="21","Name:Sam"="29","Name:Jim"="40"

.....(not just three)

How do I extract the Name and the corresponding number and convert it in to a JSON format like the one below.
{John:21,Sam:29,Jim:40}

I can extract the name and the number, but what I'm not sure about is how I can change it to the JSON format above.

NB: I need to assign this json formatted string to a variable using eval.
eval new_var=[search that generate the JSON formatted string]

0 Karma
1 Solution

_d_
Splunk Employee
Splunk Employee

What exactly are you going to use new_var for because you can export extracted name and number fields directly from the UI as JSON?

A hacky way to convert the entire quoted string of names and numbers into JSON is to essentially extract it wholesale and apply several rex commands to shape it into the correct form:

Field test: "Name:John"="21","Name:Sam"="29","Name:Jim"="40"
Search: | rex field=test mode=sed "s/(\"Name:)|(\")//g" | rex field=test mode=sed "s/=/:/g" | rex field=test mode=sed "s/^/{/" | rex field=test mode=sed "s/$/}/g"

Full example:

| stats count | eval test="\"Name:John\"=\"21\",\"Name:Sam\"=\"29\",\"Name:Jim\"=\"40\"" | rex field=test  mode=sed "s/(\"Name:)|(\")//g" | rex field=test  mode=sed "s/=/:/g" | rex field=test  mode=sed "s/^/{/" | rex field=test  mode=sed "s/$/}/g"

Output:

{John:21,Sam:29,Jim:40} 

View solution in original post

_d_
Splunk Employee
Splunk Employee

What exactly are you going to use new_var for because you can export extracted name and number fields directly from the UI as JSON?

A hacky way to convert the entire quoted string of names and numbers into JSON is to essentially extract it wholesale and apply several rex commands to shape it into the correct form:

Field test: "Name:John"="21","Name:Sam"="29","Name:Jim"="40"
Search: | rex field=test mode=sed "s/(\"Name:)|(\")//g" | rex field=test mode=sed "s/=/:/g" | rex field=test mode=sed "s/^/{/" | rex field=test mode=sed "s/$/}/g"

Full example:

| stats count | eval test="\"Name:John\"=\"21\",\"Name:Sam\"=\"29\",\"Name:Jim\"=\"40\"" | rex field=test  mode=sed "s/(\"Name:)|(\")//g" | rex field=test  mode=sed "s/=/:/g" | rex field=test  mode=sed "s/^/{/" | rex field=test  mode=sed "s/$/}/g"

Output:

{John:21,Sam:29,Jim:40} 

nibinabr
Communicator

Your solution solved my problem. But please do let know if you have a better way to get this done.

0 Karma

nibinabr
Communicator

I'm using a splunk app which has a variable that accepts some data in the form of a JSON object (eval app_variable=new_var). What I was trying to do is to convert the data in to a JSON format. Please let me know if you have a better way to do this.

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 ...

Event Series: Level up your SOC: Advancing with Splunk Enterprise Security

AI has fundamentally raised the stakes for security operations, and this three-part series is your guide to ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...