Splunk Dev

Get only parsed JSON fields using spath

nagar57
Communicator

I have a below JSON

Recalibration Stats json : {"modelid" : "30013", "champion_gini" : 0.8274502273019728, "recalibResult" : "CASE I Champion Retained", "challenger_gini" : 0.8013221831674033, "recalibDate" : "2020-05-01"}

 

Now to get the JSON fields I have to explicitly mention field names using table/fields. My JSON can get different fields from source so I want to get only parsed fields from JSON by not explicitly mentioning their names. Using below query is giving me all the unwanted fields as well.

index = abx sourcetype = gmdevops_rome source="/axp/gnics/orchestra/dev/romedata/logs/model_run_qc.log" "Recalibration Stats json"
| rex field=_raw "Recalibration Stats json : (?<recalib_stats>.+)"
| spath input=recalib_stats
| table *

Labels (2)
Tags (2)
0 Karma

nagar57
Communicator

It'll be like:

index = abx sourcetype = gmdevops_rome source="/axp/gnics/orchestra/dev/romedata/logs/model_run_qc.log" "Recalibration Stats json"
| rex field=_raw "Recalibration Stats json : (?<recalib_stats>.+)"
| fields recalib_stats
| spath input=recalib_stats
| fields - recalib_stats, _raw, _time
| table *
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Yes, removing _raw and _time as well. The important step is the selecting of recalib_stats field after the rex which removes the bulk of the other fields - the special fields beginning with an underscore have to be remove individually.

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Not sure what the question is? Are you trying to remove the "unwanted" fields without mentioning which fields are wanted?

0 Karma

nagar57
Communicator

Yes, I want only parsed fields from JSON. If I use | table * then I want only those fields that are present in JSON. Unwanted fields are date_hour, _raw, _time , date_second etc.

Tags (1)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Remove everything apart from recalib_stats after the rex, then remove recalib_stats after the spath

index = abx sourcetype = gmdevops_rome source="/axp/gnics/orchestra/dev/romedata/logs/model_run_qc.log" "Recalibration Stats json"
| rex field=_raw "Recalibration Stats json : (?<recalib_stats>.+)"
| fields recalib_stats
| spath input=recalib_stats
| fields - recalib_stats
| table *

 

0 Karma
Get Updates on the Splunk Community!

Developer Spotlight with Paul Stout

Welcome to our very first developer spotlight release series where we'll feature some awesome Splunk ...

State of Splunk Careers 2024: Maximizing Career Outcomes and the Continued Value of ...

For the past four years, Splunk has partnered with Enterprise Strategy Group to conduct a survey that gauges ...

Data-Driven Success: Splunk & Financial Services

Splunk streamlines the process of extracting insights from large volumes of data. In this fast-paced world, ...