<?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: Dynamically select json object in json array in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Dynamically-select-json-object-in-json-array/m-p/499742#M85166</link>
    <description>&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval json_field = "{\"header\": {\"index_number\": 1}, \"rows\": [{\"carrot\": 108, \"apple\": 29}, {\"carrot\": 12, \"apple\": 44}, {\"carrot\": 54, \"apple\": 23}, {\"carrot\": 67, \"apple\":9}]}" 
| spath input=json_field path=header{}.index_number output=index_number 
| spath input=json_field path=rows{} output=rows 
| eval _raw=mvindex(rows,index_number - 1) 
| kv 
| table apple carrot
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 01 Feb 2020 00:00:33 GMT</pubDate>
    <dc:creator>to4kawa</dc:creator>
    <dc:date>2020-02-01T00:00:33Z</dc:date>
    <item>
      <title>Dynamically select json object in json array</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Dynamically-select-json-object-in-json-array/m-p/499741#M85165</link>
      <description>&lt;P&gt;I'm looking to extract a JSON object from a JSON array using a dynamic index number ( based on data in another part of the JSON )&lt;/P&gt;

&lt;P&gt;Simplified JSON example........&lt;/P&gt;

&lt;P&gt;{&lt;BR /&gt;
  "header":&lt;BR /&gt;
    {"index_number": 2},&lt;BR /&gt;
  "rows": [&lt;BR /&gt;
    {"carrot": 108, "apple": 29},&lt;BR /&gt;
    {"carrot": 12, "apple": 44},&lt;BR /&gt;
    {"carrot": 54, "apple": 23},&lt;BR /&gt;
    {"carrot": 67, "apple":9}&lt;BR /&gt;
  ]&lt;BR /&gt;
}&lt;/P&gt;

&lt;P&gt;In this example the value from the field index_number is used to select the correct JSON object from the JSON array. The index_number is not a static number so the JSON object returned from the JSON array will change. Using the spath command I can statically extract a JSON object but not do it dynamically using the value in index_number as path=rows{index_number_to_use} does not work.&lt;/P&gt;

&lt;P&gt;Example SPL.......&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval json_field = "{\"header\": {\"index_number\": 1}, \"rows\": [{\"carrot\": 108, \"apple\": 29}, {\"carrot\": 12, \"apple\": 44}, {\"carrot\": 54, \"apple\": 23}, {\"carrot\": 67, \"apple\":9}]}" 
| spath input=json_field output=index_number_to_use path=header.index_number
| spath input=json_field output=row_found path=rows{1}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Any ideas on how this could be achieved?&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 03:58:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Dynamically-select-json-object-in-json-array/m-p/499741#M85165</guid>
      <dc:creator>msivill_splunk</dc:creator>
      <dc:date>2020-09-30T03:58:50Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically select json object in json array</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Dynamically-select-json-object-in-json-array/m-p/499742#M85166</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval json_field = "{\"header\": {\"index_number\": 1}, \"rows\": [{\"carrot\": 108, \"apple\": 29}, {\"carrot\": 12, \"apple\": 44}, {\"carrot\": 54, \"apple\": 23}, {\"carrot\": 67, \"apple\":9}]}" 
| spath input=json_field path=header{}.index_number output=index_number 
| spath input=json_field path=rows{} output=rows 
| eval _raw=mvindex(rows,index_number - 1) 
| kv 
| table apple carrot
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 01 Feb 2020 00:00:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Dynamically-select-json-object-in-json-array/m-p/499742#M85166</guid>
      <dc:creator>to4kawa</dc:creator>
      <dc:date>2020-02-01T00:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically select json object in json array</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Dynamically-select-json-object-in-json-array/m-p/499743#M85167</link>
      <description>&lt;P&gt;Thank you @to4kawa I was able to create a variation based on your answer that worked.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2020 08:38:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Dynamically-select-json-object-in-json-array/m-p/499743#M85167</guid>
      <dc:creator>msivill_splunk</dc:creator>
      <dc:date>2020-02-03T08:38:38Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically select json object in json array</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Dynamically-select-json-object-in-json-array/m-p/499744#M85168</link>
      <description>&lt;P&gt;you're welcome. happy splunking,too.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2020 12:59:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Dynamically-select-json-object-in-json-array/m-p/499744#M85168</guid>
      <dc:creator>to4kawa</dc:creator>
      <dc:date>2020-02-03T12:59:19Z</dc:date>
    </item>
  </channel>
</rss>

