Splunk Search

How to extract data from log message data using rex field=_raw?

bharath_hk12
Explorer

Hi,

I have logger statements like below:

Event data - {"firstName":"John","lastName":"Doe"}

 

My query needs <rex-statement> where double quotes (") in the logs are parsed and the two fields are extracted in a table:

index=my-index "Event data -" | rex <rex-statement> | fields firstName, lastName | table firstName, lastName 

 

Please let me know what <rex-statement> do I have to put. Thanks in advance.

 

Labels (3)
0 Karma
1 Solution

yuanliu
SplunkTrust
SplunkTrust

Do you mean to say that the raw event data contains extra strings such as "Event data -"?  Is there any additional strings after the closing curly bracket?  Is there any other opening curly bracket before the one shown in your mock data?  If none of those exist, simply do

 

| rex "(?<json>{.+)"
| spath input=json

 

Here is an emulation using your mock data:

 

| makeresults
| eval _raw = "Event data - {\"firstName\":\"John\",\"lastName\":\"Doe\"}"
| rex "(?<json>{.+)"
| spath input=json
| fields - _*

 

Play with it and compare with real data.  The emulation should give

firstNamejsonlastName
John{"firstName":"John","lastName":"Doe"}Doe

View solution in original post

bharath_hk12
Explorer

There are some 15-20 fields within JSON, but I want to extract only 3-4 of them. So if spath serves the purpose, good for me

0 Karma

bharath_hk12
Explorer

Thanks for your response @gcusello. Could you suggest how to use spath for the same json log? How to extract json out of the log statement and further individual fields.

I am not able to share the actual logs, since they are client specific.

0 Karma

inventsekar
Ultra Champion

>>> I am not able to share the actual logs, since they are client specific.

to use the spath command, we should check the json format. 

so, pls remove all client specific things like ip address, hostnames, usernames, etc.. replace them with sample data... like firstname:adam, etc.. and paste the json.. 

0 Karma

bharath_hk12
Explorer

I have already done that in the main question. 

Here's the sample, just that the fields will be more:

 

Event data - {"firstName":"John","lastName":"Doe"}

0 Karma

yuanliu
SplunkTrust
SplunkTrust

Do you mean to say that the raw event data contains extra strings such as "Event data -"?  Is there any additional strings after the closing curly bracket?  Is there any other opening curly bracket before the one shown in your mock data?  If none of those exist, simply do

 

| rex "(?<json>{.+)"
| spath input=json

 

Here is an emulation using your mock data:

 

| makeresults
| eval _raw = "Event data - {\"firstName\":\"John\",\"lastName\":\"Doe\"}"
| rex "(?<json>{.+)"
| spath input=json
| fields - _*

 

Play with it and compare with real data.  The emulation should give

firstNamejsonlastName
John{"firstName":"John","lastName":"Doe"}Doe

bharath_hk12
Explorer

Thanks @yuanliu, I was able to use this to derive from my actual logs

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @bharath_hk12 ,

at first, this seems to be a json log, so you can use the spath command (https://docs.splunk.com/Documentation/Splunk/9.1.2/SearchReference/Spath).

Anyway, you could use a regex like the following:

| rex "\"firstName\":\"(?<firstname>[^\"]+)\".\"lastName\":\"(?<lastName>[^\"]+)"

that you can test at https://regex101.com/r/XwqxZB/1

to be more sure, you should share some complete samples of your logs (not partial).

Ciao.

Giuseppe

Get Updates on the Splunk Community!

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

March Community Office Hours Security Series Uncovered!

Hello Splunk Community! In March, Splunk Community Office Hours spotlighted our fabulous Splunk Threat ...

Stay Connected: Your Guide to April Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars in April. This post ...