Splunk Search

Extract Fields from JSON

felipesodre
Path Finder

Hi Everyone. Thanks in advance for any help.

I am trying to extract some fields (Status, RecordsPurged)  from a JSON on the following _raw text:

 

 

 

{"": "INFO : 2020-07-09T01:11:08Z : [database@test.com]: {\"Purging_Results_Test\": {\"NewPurging\": 1, \"Status\":\"Successful\", \"VacuumEnabled\": true, \"RecordsPurged\": 6646, \"StartTime\":\"8-Jul-2020 18:03:07\", \"EndTime\":\"8-Jul-2020 18:11:08\", \"Duration(min)\":8.02}}"} 

 

 

 

Any Ideas that might help me out?

 

Thank you so much.

Labels (3)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

You mean like this?

| makeresults 
| eval _raw="{\"\": \"INFO : 2020-07-09T01:11:08Z : [database@test.com]: {\"Purging_Results_Test\": {\"NewPurging\": 1, \"Status\":\"Successful\", \"VacuumEnabled\": true, \"RecordsPurged\": 6646, \"StartTime\":\"8-Jul-2020 18:03:07\", \"EndTime\":\"8-Jul-2020 18:11:08\", \"Duration(min)\":8.02}}\"}"
| rex "Purging_Results_Test\": (?<PurgingResultsTest>.*?})"
| spath input=PurgingResultsTest
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

Normally, one uses spath to parse JSON, but it doesn't like your sample text.  So rex will do, instead.

... | rex "Status\":\"(?<Status>\w+)" | rex "RecordsPurged\": (?<RecordsPurged>\d+)"

 

---
If this reply helps you, Karma would be appreciated.

felipesodre
Path Finder

Hi, sorry for the late reply and thanks for your help.

 

For some reason your code works for some fields and others don't. Please see the image attached.

 

Any ideas that might help?

 

Screen Shot 2020-07-10 at 12.49.06 PM.png

 

Is it possible to get the JSON of Purging_Results: {all content in here} using regex and then navigate to the fields using a spath?

 

Tags (1)
0 Karma

richgalloway
SplunkTrust
SplunkTrust

You mean like this?

| makeresults 
| eval _raw="{\"\": \"INFO : 2020-07-09T01:11:08Z : [database@test.com]: {\"Purging_Results_Test\": {\"NewPurging\": 1, \"Status\":\"Successful\", \"VacuumEnabled\": true, \"RecordsPurged\": 6646, \"StartTime\":\"8-Jul-2020 18:03:07\", \"EndTime\":\"8-Jul-2020 18:11:08\", \"Duration(min)\":8.02}}\"}"
| rex "Purging_Results_Test\": (?<PurgingResultsTest>.*?})"
| spath input=PurgingResultsTest
---
If this reply helps you, Karma would be appreciated.

felipesodre
Path Finder

It just worked perfectly.

Thank you so much, appreciate it!!!!

0 Karma
Get Updates on the Splunk Community!

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

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