Getting Data In

Is it possible to parse an extracted field as json if the whole log line isn't json?

stevennoble
Explorer

If I have a line of my logs that look something like

[2013-10-18 23:36:50.785476] {"message":"some message", "headers": {"a": 1, "b": 2}}

is there a way I can use splunk's regex extraction to separate the timestamp from the json, then use splunk's json extraction to to extract fields from the json?

Tags (1)
1 Solution

_d_
Splunk Employee
Splunk Employee

You can't use KV_MODE=json in props.conf because in its current state the event is not fully json. Additionally you can't extract the rest of the messages and then use the same setting on it (again, from props.conf). However, you can do it inline with spath.

Extract the whole json message in a field called, say, my_field, then use spath:

...| spath input=my_field

View solution in original post

kamermans
Path Finder

You can remove the string leader using any the following methods:

  1. Remove leader by pretending it's a line breaker

    LINE_BREAKER=((:?^|\n).+?){
    SHOULD_LINEMERGE=false

  2. Removing the leader with SEDCMD:

    SEDCMD-StripHeader=s/^[^{]+//

  3. Removing the leader via a transform on _raw:

    ; in transforms.conf:
    [StripSyslog]
    REGEX = ^[^{]+(.*)$
    FORMAT = $1
    DEST_KEY = _raw


    ; in props.conf:
    TRANSFORMS-StripSyslog = StripSyslog

All these methods will work with KV_MODE=json, but note that currently they will not work with INDEXED_EXTRACTIONS=json.

prees
Explorer

Second what @wsnyder2 said. Is it possible to do this and still retain the info from the header?

EDIT: or is it possible to pre-transform them based on the header then apply this transform?

0 Karma

wsnyder2
Path Finder

Can I do this and keep my header?

0 Karma

_d_
Splunk Employee
Splunk Employee

You can't use KV_MODE=json in props.conf because in its current state the event is not fully json. Additionally you can't extract the rest of the messages and then use the same setting on it (again, from props.conf). However, you can do it inline with spath.

Extract the whole json message in a field called, say, my_field, then use spath:

...| spath input=my_field

wsnyder2
Path Finder

This is a bummer ... We have jason as part of an event. Would love to have field extraction happen under the hood, e.g. in props.conf or transforms.conf.
Seems like there is a way based on postings in this thread. (?)

0 Karma

prees
Explorer

Was any one able to do this? as originally described, ie. without using spath?

0 Karma

_d_
Splunk Employee
Splunk Employee

Inline in a search. You can follow the ..|spath input=my_field with any other search commands. More details and examples here: http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Spath

0 Karma

stevennoble
Explorer

This is something that I'd use in a search? Or can this be applied to a log source?

0 Karma
Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...