Splunk Search

How to parse string JSON along with actual JSON?

JChris_
Path Finder

I have the following log in Splunk:

 

{
    "tags":{
        "app":"foobar",
        "ou":"internal"
    },
    "log":"{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}",
    "timestamp":"2022-04-21T17:00:00.000Z"
}

 

 

I know I can parse the string JSON into actual JSON and replace the _raw like this:

 

index=my_index_name
| eval _raw=log

 

But, if I use the SPL above, the timestamp and tags keys would be deleted from the _raw, that's not what I want.

 

I want to use SPL to parse it in a way where the _raw equals to:

 

 

{
    "tags":{
        "app":"foobar",
        "ou":"internal"
    },
    "log": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    },
    "timestamp":"2022-04-21T17:00:00.000Z"
}

 

 

Labels (2)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

| eval _raw=replace(_raw,"\\\\","")
| eval _raw=replace(_raw,"\"{","{")
| eval _raw=replace(_raw,"}\"","}")
| spath

View solution in original post

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

| eval _raw=replace(_raw,"\\\\","")
| eval _raw=replace(_raw,"\"{","{")
| eval _raw=replace(_raw,"}\"","}")
| spath
0 Karma

PickleRick
SplunkTrust
SplunkTrust

Ok. So you have a json-formatted value inside your json event.

You can approach it from two different angles.

1) Explicitly use spath on that value.

<your_search>
| spath input=log

And I think it's the easiest solution.

2) "Rearrange" your event a bit - remember the old value of _raw, replace it, let Splunk parse it and then restore old _raw. A bit confusing, I know 😉

<your_search>
| eval oldraw=_raw
| eval _raw=log
| extract
| eval _raw=oldraw
| fields - oldraw

Haven't tried it but should work (but it's not a pretty solution).

0 Karma

JChris_
Path Finder

I thought of using spath, it does work but does not override the _raw. I wanted the _raw to also be a complete JSON.

0 Karma

PickleRick
SplunkTrust
SplunkTrust

Then you'd have to manipulate the event manually. Which could probably be possible in some specific cases but it'd be hard to do in general case. Firstly you'd have to remove the original contents of the field, which is not simply "anything between the quotes" because you'd have to account for escaped quotes (possibly on many levels of nested strings).

But the more importantly - you'd have to reconstruct the json structure. While you have some json_*() functions, you have to know the structure beforehand to create/modify a json object. So if you can have anything in your "log" field... well, I don't see it working.

0 Karma
Get Updates on the Splunk Community!

.conf24 | Day 0

Hello Splunk Community! My name is Chris, and I'm based in Canberra, Australia's capital, and I travelled for ...

Enhance Security Visibility with Splunk Enterprise Security 7.1 through Threat ...

(view in My Videos)Struggling with alert fatigue, lack of context, and prioritization around security ...

Troubleshooting the OpenTelemetry Collector

  In this tech talk, you’ll learn how to troubleshoot the OpenTelemetry collector - from checking the ...