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!

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...