I've a log to extract key value pairs from but I'm unable to extract all of them
Sample log:
2017-12-19 19:33:55,848 LL="DEBUG" CR="abq_asdaasd_adasdq_DAS" RE="abd" DE="abc" TR="tomcat-http--41" LN="xyz" TYPE="IN-RESPONSE" BODY="{"key":{"key1":"value1","key2":"value2","dateAndTimeTransmission":"2017-12-19T19:33:54.000Z","key3":"value3","key4":"value4","key5":"value5","key6":"value6","key":2,"key":{"key7":"value7","key8":"value8","key9":"value9"},"key":{"key":[{"key10":"value10","key11":"value12","key13":"value13"},{"key14":"value14","key":"value","key":"value"}]},"key":"value","key":"value"}}"
I'm using the query ----- > index=xyz ( $searchKey$ ) | extract pairdelim="," kvdelim=":" | dedup key ------ then followed by putting the value in the table.
I require key10,value10 and key11,value11 I'm able to extract key11,value11 but my query is not extracting key10,value10 .. maybe due to the [{
Can someone please help?
@sagar1905, you should be using spath
command to parse and extract JSON data. Try the following run anywhere search based on your data. As per your data BODY should show up during field discovery with the complete JSON data. If not you might have to create a regular expression to extract complete JSON data to a field first using rex command.
| makeresults
| eval BODY="{\"key\":{\"key1\":\"value1\",\"key2\":\"value2\",\"dateAndTimeTransmission\":\"2017-12-19T19:33:54.000Z\",\"key3\":\"value3\",\"key4\":\"value4\",\"key5\":\"value5\",\"key6\":\"value6\",\"key\":2,\"key\":{\"key7\":\"value7\",\"key8\":\"value8\",\"key9\":\"value9\"},\"key\":{\"key\":[{\"key10\":\"value10\",\"key11\":\"value12\",\"key13\":\"value13\"},{\"key14\":\"value14\",\"key\":\"value\",\"key\":\"value\"}]},\"key\":\"value\",\"key\":\"value\"}}"
| spath input=BODY
Refer to spath documentation for details: http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Spath
The keys are fixed but the values aren't, will the above query still work? And the keys also get changed sometimes, is there a general way to extract this?
@sagar1905, yes it should work. Spath
command is used to traverse XML and JSON to extract Key Value pairs. Do check the command reference. Instead of first two commands i.e. | makeresults and | eval BODY=...
, you can put your Base search to extract the JSON data field i.e. BODY as per your question an then feed the same to spath command to get the extracted fields.
Please try out and confirm!