Splunk Search

Multivalue Extraction

cquinney
Communicator

I have the following set of data within each event:

stack_trace: [ [-]
{ [-]
class_name: FOO
file_name: BAR
line_number: -2
method_name: WALK
}
{ [-]
class_name: FOO2
file_name: BAR2
line_number: 1356
method_name: JUMP
}
{ [-]
class_name: FOO
file_name: BAR
line_number: 808
method_name: SKIP
}
]

I want to extract only the first method_name within the stack (| spath "stack_trace{}.method_name" | search "stack_trace{}.method_name"=WALK), which can change from event to event. I've tried using mvindex but I'm having no success. Any suggestions would be greatly appreciated.

0 Karma

to4kawa
Ultra Champion
| makeresults
| eval _raw="{\"stack_trace\":[{\"class_name\":\"FOO\",\"file_name\":\"BAR\",\"line_number\":\"-2\",\"method_name\":\"WALK\"},{\"class_name\":\"FOO2\",\"file_name\":\"BAR2\",\"line_number\":\"1356\",\"method_name\":\"JUMP\"},{\"class_name\":\"FOO\",\"file_name\":\"BAR\",\"line_number\":\"808\",\"method_name\":\"SKIP\"}]}"
| rex "method_name.*?(?<method_name>\w+)"

If you want only first method_name, try rex . Regex works by order.

0 Karma

dmarling
Builder

Using mvindex will work for you use case, but you will need to wrap the field name in single quotes since there are special characters in the field name that mean concatenation when using where or eval: . Using this eval will get the first method_name in the stack: | eval first_method_name=mvindex('stack_trace{}.method_name', 0) Here's a run anywhere example using your own data to demonstrate it:

| makeresults count=1
| eval data="{\"stack_trace\":[{\"class_name\":\"FOO\",\"file_name\":\"BAR\",\"line_number\":\"-2\",\"method_name\":\"WALK\"},{\"class_name\":\"FOO2\",\"file_name\":\"BAR2\",\"line_number\":\"1356\",\"method_name\":\"JUMP\"},{\"class_name\":\"FOO\",\"file_name\":\"BAR\",\"line_number\":\"808\",\"method_name\":\"SKIP\"}]}"
| spath input=data
| eval first_method_name=mvindex('stack_trace{}.method_name', 0)
If this comment/answer was helpful, please up vote it. Thank you.

cquinney
Communicator

Thank you! The eval works great, I was using double quotes instead of single, great catch!

0 Karma

dmarling
Builder

I'm glad I could help you! Please make sure to also accept the answer. Thank you!

If this comment/answer was helpful, please up vote it. Thank you.
0 Karma
Get Updates on the Splunk Community!

Splunk APM & RUM | Upcoming Planned Maintenance

There will be planned maintenance of the streaming infrastructure for Splunk APM and Splunk RUM in the coming ...

Part 2: Diving Deeper With AIOps

Getting the Most Out of Event Correlation and Alert Storm Detection in Splunk IT Service Intelligence   Watch ...

User Groups | Upcoming Events!

If by chance you weren't already aware, the Splunk Community is host to numerous User Groups, organized ...