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
Get Updates on the Splunk Community!

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...