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.
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
firstName | json | lastName |
John | {"firstName":"John","lastName":"Doe"} | Doe |
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
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.
>>> 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..
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"}
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
firstName | json | lastName |
John | {"firstName":"John","lastName":"Doe"} | Doe |
Thanks @yuanliu, I was able to use this to derive from my actual logs
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