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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...