Getting Data In

How to extract unique Json String field value?

srinim1234
Engager

Hi,

I have the following JSON String logs. I would like to extract JSON unique field values. It should go over all the message fields and extract specific field values from a JSON array("name") and unique them. Could someone help with Splunk query?

 

Raw log

{
"@timestamp": "2022-03-28T07:38:45.123+00:00",
"message": "request - {\"metrics\":[{\"name\":\"m1\",\"downsample\":\"sum\"},{\"name\":\"m2\",\"downsample\":\"sum\"},{\"name\":\"m1\",\"downsample\":\"sum\"}]}"
}

JSON

 {
"metrics": [{
"name": "m1",
"aggregator": "sum",
}, {
"name": "m2",
"downsample": "sum"
}, {
"name": "m1",
"downsample": "sum"
}]
}

 

Expected Output:

 

m1
m2 
...

 

Labels (1)
0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

@srinim1234 

Can you please try below search?

YOUR_SEARCH
| rex field=message \"name\":\"(?<name>\w+)\" max_match=0
| mvexpand name
| table name

 

I suggest above search for your requirement. 

Below search for learning purpose as another way of achieving same output.

YOUR_SEARCH
| rex field=message "request - (?<data>.*)" | rename data as _raw | kv
|mvexpand metrics{}.name
| table metrics{}.name

 

Thanks
KV


If any of my reply helps you to solve the problem Or gain knowledge, an upvote would be appreciated.

View solution in original post

kamlesh_vaghela
SplunkTrust
SplunkTrust

@srinim1234 

Can you please try below search?

YOUR_SEARCH
| rex field=message \"name\":\"(?<name>\w+)\" max_match=0
| mvexpand name
| table name

 

I suggest above search for your requirement. 

Below search for learning purpose as another way of achieving same output.

YOUR_SEARCH
| rex field=message "request - (?<data>.*)" | rename data as _raw | kv
|mvexpand metrics{}.name
| table metrics{}.name

 

Thanks
KV


If any of my reply helps you to solve the problem Or gain knowledge, an upvote would be appreciated.

srinim1234
Engager

Thank you! This helped!

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| rex max_match=0 "\\\\\"name\\\\\":\\\\\"(?<name>[^\\\\]+)"
| eval name=mvdedup(name)
Get Updates on the Splunk Community!

Join Us at the Builder Bar at .conf24 – Empowering Innovation and Collaboration

What is the Builder Bar? The Builder Bar is more than just a place; it's a hub of creativity, collaboration, ...

Combine Multiline Logs into a Single Event with SOCK - a Guide for Advanced Users

This article is the continuation of the “Combine multiline logs into a single event with SOCK - a step-by-step ...

Everything Community at .conf24!

You may have seen mention of the .conf Community Zone 'round these parts and found yourself wondering what ...