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
SplunkTrust
SplunkTrust

>>> 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

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...