All Apps and Add-ons

Why spath is not working when there is text before and after json data.

jw44250
New Member

index=index1 sourcetype=test1 |spath output=myfield path=Student{}.SubjectDetails{}.type |table myfield, Class

{
"Class": "11",
"date": "05/16/2016",
"Student": [
{
"RollNo": "1234",
"SubjectDetails": [
{
"type": "Mandatory",
"startTime": "05/16/2016 21:30",
"endTime": "05/16/2016 22:00",
"name": "English"
},
{
"type": "Optional",
"startTime": "05/16/2016 20:00",
"endTime": "05/16/2016 21:30",
"name": "Math"
}
]
}
]
}

the above splunk query can work if the result is only contains JSON but it will not work when before and after there text with before and after JSON Data.

got data from external x machine.
{
"Class": "11",
"date": "05/16/2016",
"Student": [
{
"RollNo": "1234",
"SubjectDetails": [
{
"type": "Mandatory",
"startTime": "05/16/2016 21:30",
"endTime": "05/16/2016 22:00",
"name": "English"
},
{
"type": "Optional",
"startTime": "05/16/2016 20:00",
"endTime": "05/16/2016 21:30",
"name": "Math"
}
]
}
]
}
got data from external x machine.

0 Karma

niketn
Legend

@jw44250, use replace() eval function with regular expression to remove got data from external x machine. from _raw data. You should also think about dropping the data with regular expression REGEX using props.conf and transforms.conf before indexing: http://docs.splunk.com/Documentation/Splunk/latest/Forwarding/Routeandfilterdatad.

|  makeresults
|  eval _raw="got data from external x machine.
{
\"Class\": \"11\",
\"date\": \"05/16/2016\",
\"Student\": [
{
\"RollNo\": \"1234\",
\"SubjectDetails\": [
{
\"type\": \"Mandatory\",
\"startTime\": \"05/16/2016 21:30\",
\"endTime\": \"05/16/2016 22:00\",
\"name\": \"English\"
},
{
\"type\": \"Optional\",
\"startTime\": \"05/16/2016 20:00\",
\"endTime\": \"05/16/2016 21:30\",
\"name\": \"Math\"
}
]
}
]
}
got data from external x machine."
| eval _raw=replace(_raw,"got data from external \w+ machine\.", "")
| spath
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

jw44250
New Member

replace wont work as the text keeps changes ...the text lenght can be very as well.

0 Karma

niketn
Legend

@jw44250 , as mentioned above replace() uses regular expression. If you would notice based on your sample data I have added \w+ for Machine name with word characters. You can change regular expression based on Regular Expression pattern of data before and after JSON.

If you are not able to figure out the regular expression yourself, you will have to provide some sample events of data before and after JSON. (Start and End of JSON can also be used for Regular Expression match).

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...