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!

Detecting Remote Code Executions With the Splunk Threat Research Team

WATCH NOWRemote code execution (RCE) vulnerabilities pose a significant risk to organizations. If exploited, ...

Enter the Splunk Community Dashboard Challenge for Your Chance to Win!

The Splunk Community Dashboard Challenge is underway! This is your chance to showcase your skills in creating ...

.conf24 | Session Scheduler is Live!!

.conf24 is happening June 11 - 14 in Las Vegas, and we are thrilled to announce that the conference catalog ...