<?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 Re: Extracting data from complicated JSON, match a value in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-data-from-complicated-JSON-match-a-value/m-p/252134#M96117</link>
    <description>&lt;P&gt;Wow, this was a lot of steps, I am very grateful,  I made a slight tweak:&lt;/P&gt;

&lt;P&gt;after the &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rename nameValues{}.name as MyName, nameValues{}.value as MyValue
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I added&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| search MyName=keyNameIwasLookingFor AND MyValue=valueIWasLookingFor | table MyName, MyValue
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and was able to limit to only include entries that contained keyNameIwasLookingFor=valueIWasLookingFor&lt;/P&gt;</description>
    <pubDate>Sun, 29 Jan 2017 03:10:44 GMT</pubDate>
    <dc:creator>CanadianTrevorS</dc:creator>
    <dc:date>2017-01-29T03:10:44Z</dc:date>
    <item>
      <title>Extracting data from complicated JSON, match a value</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-data-from-complicated-JSON-match-a-value/m-p/252131#M96114</link>
      <description>&lt;P&gt;I have JSON in the following format:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[
    {
      "nameValues": [], 
      "offeringId": "a"
    }, 
    {
      "nameValues": [
        {
          "name": "key1", 
          "value": "true"
        }, 
        {
          "name": "key2", 
          "value": "value2"
        }
      ], 
      "offeringId": "b"
    }
]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I am looking to match a create a field with the value of one of the one the name value pairs, that matches the offeringId=b. The output being &lt;STRONG&gt;key1&lt;/STRONG&gt;=true. All the keyvalue pairs in nameValues would obviously also suffice.&lt;/P&gt;

&lt;P&gt;I've struggled with spath, but not sure that you can select a specific offeringId=b like you can do in xpath. Or would I be better off attempting this via regex?&lt;/P&gt;

&lt;P&gt;Any help would be much appreciated.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 18:47:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-data-from-complicated-JSON-match-a-value/m-p/252131#M96114</guid>
      <dc:creator>CanadianTrevorS</dc:creator>
      <dc:date>2017-01-24T18:47:14Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting data from complicated JSON, match a value</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-data-from-complicated-JSON-match-a-value/m-p/252132#M96115</link>
      <description>&lt;P&gt;&lt;STRONG&gt;THIS ANSWER, THOUGH SENSIBLE, WAS WRONG.  SEE THE OTHER ONE.&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;Unfortunately, since the offering containers are unnamed, there is no way to differentiate which offering a particular key came from &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults | eval source="[{\"nameValues\":[],\"offeringId\":\"a\"},{\"nameValues\":[{\"name\":\"key1\",\"value\":\"true\"},{\"name\":\"key2\",\"value\":\"value2\"}],\"offeringId\": \"b\"}]"
| spath input=source 
| table source  {}.nameValues{}.name {}.nameValues{}.value {}.offeringId
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;outputs something like this &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source     {}.nameValues{}.name {}.nameValues{}.value   {}.offeringId

(the JSON)  key1                   true                    a

            key2                   value2                  b   
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Since there is no cardinality that relates the OfferingId with the keys, there's nothing there to help us pull them out, as far as I can see, in the current JSON structure.&lt;/P&gt;

&lt;P&gt;It might be seen more clearly in this example, where there are five keys for the two OfferingIds&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults | eval source=
"[{\"nameValues\":[
       {\"name\":\"key3\",\"value\":\"maybe\"},
       {\"name\":\"key7\",\"value\":\"idunno\"},
       {\"name\":\"key5\",\"value\":\"letmecheckmynotes\"}
       ],
   \"offeringId\":\"a\"},
  {\"nameValues\":[
       {\"name\":\"key1\",\"value\":\"true\"},
       {\"name\":\"key2\",\"value\":\"value2\"}
       ],
   \"offeringId\": \"b\"}]"
 | spath input=source 
 | table source  {}.nameValues{}.name {}.nameValues{}.value {}.offeringId
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Jan 2017 21:00:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-data-from-complicated-JSON-match-a-value/m-p/252132#M96115</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-01-24T21:00:30Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting data from complicated JSON, match a value</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-data-from-complicated-JSON-match-a-value/m-p/252133#M96116</link>
      <description>&lt;P&gt;Okay, I lied, it can be done.  It just has to be done in steps.&lt;/P&gt;

&lt;P&gt;Here's the whole code, assuming you were looking for the value of "key2" for offeringId="b".  A full explanation of what I've done here follows that.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults | eval source="[{\"nameValues\":[],\"offeringId\":\"a\"},{\"nameValues\":[{\"name\":\"key1\",\"value\":\"true\"},{\"name\":\"key2\",\"value\":\"value2\"}],\"offeringId\": \"b\"}]"
 | spath input=source path="{}"
 | rename "{}" as mystuff
 | mvexpand mystuff 
 | spath input=mystuff
 | table source mystuff offeringId nameValues{}.name nameValues{}.value
 | where offeringId="b"
 | rename nameValues{}.name as MyName, nameValues{}.value as MyValue
 | eval MyNameValue=mvzip(MyName, MyValue,"=")
 | table offeringId MyNameValue
 | mvexpand MyNameValue
 | where match(MyNameValue,"key2")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;P&gt;This just creates the test data.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | makeresults | eval source="[{\"nameValues\":[],\"offeringId\":\"a\"},{\"nameValues\":[{\"name\":\"key1\",\"value\":\"true\"},{\"name\":\"key2\",\"value\":\"value2\"}],\"offeringId\": \"b\"}]"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This spath with a path set to {} will extract the data at that first, highest level.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | spath input=source path="{}"
 | rename "{}" as mystuff
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now we use mvexpand to break up the JSON into individual transactions at that same, highest level, and then run spath again on the result.  At this level, the key/value pairs are connected to the offeringId.   We throw away everything that wasn't offeringId = "b".&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| mvexpand mystuff 
| spath input=mystuff
| table source mystuff offeringId nameValues{}.name nameValues{}.value
| where offeringId="b"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now, we change the name from the hideous JSON format to a plan name, and mvzip together the related name/value fields.  I'm formatting them here as a single field where the data looks like "key2=value2".  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rename nameValues{}.name as MyName, nameValues{}.value as MyValue
| eval MyNameValue=mvzip(MyName, MyValue,"=")
| table offeringId MyNameValue
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Finally, we expand again, and throw away all the rows that don't contain the key value we were looking for.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| mvexpand MyNameValue
| where match(MyNameValue,"key2")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Jan 2017 21:51:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-data-from-complicated-JSON-match-a-value/m-p/252133#M96116</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-01-24T21:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting data from complicated JSON, match a value</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-data-from-complicated-JSON-match-a-value/m-p/252134#M96117</link>
      <description>&lt;P&gt;Wow, this was a lot of steps, I am very grateful,  I made a slight tweak:&lt;/P&gt;

&lt;P&gt;after the &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rename nameValues{}.name as MyName, nameValues{}.value as MyValue
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I added&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| search MyName=keyNameIwasLookingFor AND MyValue=valueIWasLookingFor | table MyName, MyValue
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and was able to limit to only include entries that contained keyNameIwasLookingFor=valueIWasLookingFor&lt;/P&gt;</description>
      <pubDate>Sun, 29 Jan 2017 03:10:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-data-from-complicated-JSON-match-a-value/m-p/252134#M96117</guid>
      <dc:creator>CanadianTrevorS</dc:creator>
      <dc:date>2017-01-29T03:10:44Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting data from complicated JSON, match a value</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-data-from-complicated-JSON-match-a-value/m-p/252135#M96118</link>
      <description>&lt;P&gt;Okay, I don't think that's right.  If you already knew what the value was, then what was the point of checking the json?&lt;/P&gt;</description>
      <pubDate>Sun, 29 Jan 2017 06:37:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-data-from-complicated-JSON-match-a-value/m-p/252135#M96118</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-01-29T06:37:02Z</dc:date>
    </item>
  </channel>
</rss>

