<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to extract a value from a JSON multivalue field based on a value from another multivalue field? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239204#M71059</link>
    <description>&lt;P&gt;Greetings,&lt;/P&gt;

&lt;P&gt;I have this sample json data indexed in Splunk:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{"billId":3598,"bodyLines":
[{"bodyLineId":24246,"value":116281.200000,"caption":"Unadjusted Consumption"},{"bodyLineId":24247,"value":120653.370000,"caption":"Adjusted Consumption"}]}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I want to extract &lt;CODE&gt;bodyLines{}.value&lt;/CODE&gt; (ie. &lt;CODE&gt;120653.370000&lt;/CODE&gt;) where &lt;CODE&gt;bodyLines{}.caption=="Adjusted Consumption"&lt;/CODE&gt;.  Can someone please provide the right (eval?) syntax to extract the value?&lt;/P&gt;</description>
    <pubDate>Wed, 18 Nov 2015 15:52:00 GMT</pubDate>
    <dc:creator>suarezry</dc:creator>
    <dc:date>2015-11-18T15:52:00Z</dc:date>
    <item>
      <title>How to extract a value from a JSON multivalue field based on a value from another multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239204#M71059</link>
      <description>&lt;P&gt;Greetings,&lt;/P&gt;

&lt;P&gt;I have this sample json data indexed in Splunk:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{"billId":3598,"bodyLines":
[{"bodyLineId":24246,"value":116281.200000,"caption":"Unadjusted Consumption"},{"bodyLineId":24247,"value":120653.370000,"caption":"Adjusted Consumption"}]}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I want to extract &lt;CODE&gt;bodyLines{}.value&lt;/CODE&gt; (ie. &lt;CODE&gt;120653.370000&lt;/CODE&gt;) where &lt;CODE&gt;bodyLines{}.caption=="Adjusted Consumption"&lt;/CODE&gt;.  Can someone please provide the right (eval?) syntax to extract the value?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2015 15:52:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239204#M71059</guid>
      <dc:creator>suarezry</dc:creator>
      <dc:date>2015-11-18T15:52:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a value from a JSON multivalue field based on a value from another multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239205#M71060</link>
      <description>&lt;P&gt;How about &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    .. | spath output=blid bodyLines{}.value | spath output=blcaption bodyLines{}.caption | search blcaption="Adjusted*"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Nov 2015 16:51:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239205#M71060</guid>
      <dc:creator>sundareshr</dc:creator>
      <dc:date>2015-11-18T16:51:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a value from a JSON multivalue field based on a value from another multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239206#M71061</link>
      <description>&lt;P&gt;Here's a solution, assuming there is only one billId per event&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| spath output=value bodyLines{}.value
| spath output=caption bodyLines{}.caption
| eval zipped=mvzip(value,caption) 
| mvexpand zipped
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You'll now have a separate event for each value. You can read caption and value as a pair:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makemv delim="," zipped
| eval adjustedConsumption=if(mvindex(zipped, 1) = "Adjusted Consumption", mvindex(zipped, 0), '')
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Or create new fields for them and filter out the other results:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makemv delim="," zipped
| eval caption=mvindex(zipped, 1)
| eval value=mvindex(zipped, 0)
| search caption = "Adjusted Consumption"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Nov 2015 16:59:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239206#M71061</guid>
      <dc:creator>mrobichaud_splu</dc:creator>
      <dc:date>2015-11-18T16:59:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a value from a JSON multivalue field based on a value from another multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239207#M71062</link>
      <description>&lt;P&gt;It works!  ...but I'm gonna have nightmares about this search...&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2015 18:32:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239207#M71062</guid>
      <dc:creator>suarezry</dc:creator>
      <dc:date>2015-11-18T18:32:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a value from a JSON multivalue field based on a value from another multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239208#M71063</link>
      <description>&lt;P&gt;Glad it worked! Working with multivalue fields is often unintuitive.&lt;/P&gt;

&lt;P&gt;Sweet dreams.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2015 18:43:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239208#M71063</guid>
      <dc:creator>mrobichaud_splu</dc:creator>
      <dc:date>2015-11-18T18:43:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a value from a JSON multivalue field based on a value from another multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239209#M71064</link>
      <description>&lt;P&gt;That's a better way to create the fields than what I did, but you still need to use mvzip() and mvexpand to get the correct value.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2015 18:47:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239209#M71064</guid>
      <dc:creator>mrobichaud_splu</dc:creator>
      <dc:date>2015-11-18T18:47:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a value from a JSON multivalue field based on a value from another multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239210#M71065</link>
      <description>&lt;P&gt;Updated with sundareshr's spath command instead of rex.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2015 18:50:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239210#M71065</guid>
      <dc:creator>mrobichaud_splu</dc:creator>
      <dc:date>2015-11-18T18:50:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a value from a JSON multivalue field based on a value from another multivalue field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239211#M71066</link>
      <description>&lt;P&gt;I realize this question is old, but apparently lots of people reference it.   Handling JSON arrays in Splunk can be difficult and require many SPL commands.  And in a simple case like this, it's not too bad, but if you have to unwrap a few JSON arrays simultaneously the &lt;CODE&gt;mvzip()&lt;/CODE&gt; and &lt;CODE&gt;mvexpand&lt;/CODE&gt; approach become super tedious.&lt;/P&gt;

&lt;P&gt;If you deal with complex JSON on a regular basis, be sure to check out the &lt;A href="https://splunkbase.splunk.com/app/3237/"&gt;JMESPath app for Splunk&lt;/A&gt;.  It makes this type of extraction super easy to do in a single command.  Take a look:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| jmespath output=adjustConsumption_value "bodyLines[?caption=='Adjusted Consumption'].value"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here's a run-anywhere example for those following along at home:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults  | eval _raw="{\"billId\":3598,\"bodyLines\":[{\"bodyLineId\":24246,\"value\":116281.2,\"caption\":\"Unadjusted Consumption\"},{\"bodyLineId\":24247,\"value\":120653.37,\"caption\":\"Adjusted Consumption\"}]}"
| jmespath output=adjustConsumption_value "bodyLines[?caption=='Adjusted Consumption'].value"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Full disclosure.  I'm currently the maintainer of the JMESPath for Splunk.  I took over because the original author ran out of time, and because I think this app is awesome!&lt;/P&gt;</description>
      <pubDate>Sat, 17 Nov 2018 01:18:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-a-value-from-a-JSON-multivalue-field-based-on-a/m-p/239211#M71066</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2018-11-17T01:18:01Z</dc:date>
    </item>
  </channel>
</rss>

