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

thanks and best regards,
Sekar

PS - If this or any post helped you in any way, pls consider upvoting, thanks for reading !
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!

Dashboards: Hiding charts while search is being executed and other uses for tokens

There are a couple of features of SimpleXML / Classic dashboards that can be used to enhance the user ...

Splunk Observability Cloud's AI Assistant in Action Series: Explaining Metrics and ...

This is the fourth post in the Splunk Observability Cloud’s AI Assistant in Action series that digs into how ...

Brains, Bytes, and Boston: Learn from the Best at .conf25

When you think of Boston, you might picture colonial charm, world-class universities, or even the crack of a ...