<?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 use foreach to extract value from another json object? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-foreach-to-extract-value-from-another-json-object/m-p/642068#M222414</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to get the values from one json object using the keys from another json array. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;| makeresults&lt;BR /&gt;| eval limits=json_object("process1", json_array(123), "process2", json_array(234), "process3", json_array(0.12)), total=0&lt;BR /&gt;| eval processes = json_array("process1", "process2")&lt;BR /&gt;| eval new_data_limits = json_object()&lt;BR /&gt;| foreach processes&lt;BR /&gt;[ | eval new_data_limits = json_set(new_data_limits, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;, json_extract(limits, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;))]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) How do I capture the limits into the new_data_limits array?&lt;/P&gt;&lt;P&gt;2)&amp;nbsp;If there's multiple events similar to 'limits', how do I get the average of similar process? (i.e "process1", "process2")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TIA....&lt;/P&gt;</description>
    <pubDate>Tue, 02 May 2023 21:25:20 GMT</pubDate>
    <dc:creator>GaryZ</dc:creator>
    <dc:date>2023-05-02T21:25:20Z</dc:date>
    <item>
      <title>How to use foreach to extract value from another json object?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-foreach-to-extract-value-from-another-json-object/m-p/642068#M222414</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to get the values from one json object using the keys from another json array. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;| makeresults&lt;BR /&gt;| eval limits=json_object("process1", json_array(123), "process2", json_array(234), "process3", json_array(0.12)), total=0&lt;BR /&gt;| eval processes = json_array("process1", "process2")&lt;BR /&gt;| eval new_data_limits = json_object()&lt;BR /&gt;| foreach processes&lt;BR /&gt;[ | eval new_data_limits = json_set(new_data_limits, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;, json_extract(limits, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;))]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) How do I capture the limits into the new_data_limits array?&lt;/P&gt;&lt;P&gt;2)&amp;nbsp;If there's multiple events similar to 'limits', how do I get the average of similar process? (i.e "process1", "process2")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TIA....&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 21:25:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-foreach-to-extract-value-from-another-json-object/m-p/642068#M222414</guid>
      <dc:creator>GaryZ</dc:creator>
      <dc:date>2023-05-02T21:25:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to use foreach to extract value from another json object?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-foreach-to-extract-value-from-another-json-object/m-p/642075#M222417</link>
      <description>&lt;P&gt;| makeresults&lt;BR /&gt;| eval limits=json_object("process1", json_array(123), "process2", json_array(234), "process3", json_array(0.12)), total=0&lt;BR /&gt;| eval processes = json_array("process1", "process2")&lt;BR /&gt;| eval new_data_limits = json_object()&lt;BR /&gt;| foreach processes&lt;BR /&gt;[ | eval key = tostring(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;), value = json_extract(limits, key), new_data_limits = if(isnull(new_data_limits), json_object(key, value), json_set(new_data_limits, key, value)) ]&lt;BR /&gt;&lt;BR /&gt;get average&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;base search | stats avg(*) as * by process1, process2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 22:25:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-foreach-to-extract-value-from-another-json-object/m-p/642075#M222417</guid>
      <dc:creator>abi2023</dc:creator>
      <dc:date>2023-05-02T22:25:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to use foreach to extract value from another json object?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-foreach-to-extract-value-from-another-json-object/m-p/642080#M222420</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/256053"&gt;@abi2023&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I understand your code, and I believe that it should work. &amp;nbsp;however when I run the search, I get the following.&lt;/P&gt;&lt;P&gt;I don't see new_data_limits capturing the limit values. &amp;nbsp;I've tried rerunning the search, and have also stepped through it without the foreach loop. &amp;nbsp;I do see the results. &amp;nbsp;However when it's used in the foreach loop, the limits values aren't in the new_data_limits variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="GaryZ_1-1683068282273.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/25193iB88C7CCFA76486AA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="GaryZ_1-1683068282273.png" alt="GaryZ_1-1683068282273.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 22:59:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-foreach-to-extract-value-from-another-json-object/m-p/642080#M222420</guid>
      <dc:creator>GaryZ</dc:creator>
      <dc:date>2023-05-02T22:59:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to use foreach to extract value from another json object?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-foreach-to-extract-value-from-another-json-object/m-p/642098#M222425</link>
      <description>&lt;P&gt;By default, foreach uses &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Foreach#Optional_arguments" target="_blank" rel="noopener"&gt;multifield mode&lt;/A&gt;. &amp;nbsp;What you need is its json_array mode. &amp;nbsp;(If you have Splunk 9, that is. &amp;nbsp;Before Splunk 9, foreach only has multifield mode.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval limits=json_object("process1", json_array(123), "process2", json_array(234), "process3", json_array(0.12)), total=0
| eval processes = json_array("process1", "process2")
| eval new_data_limits = json_object()
| foreach processes mode=json_array
[ | eval new_data_limits = json_set(new_data_limits, &amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;, json_extract(limits, &amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;))]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To get average, on the other hand, I would lose that JSON array for processes because you need separate columns.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval limits=mvappend(json_object("process1", json_array(123), "process2", json_array(234), "process3", json_array(0.12)),
json_object("process1", json_array(345), "process2", json_array(678), "process3", json_array(0.12))), total=0
| mvexpand limits
```data emulation above```
| foreach process1 process2
[ eval new_&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;_limit = json_array_to_mv(json_extract(limits, "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"))]
| stats sum(total) as total avg(*) as *&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 03 May 2023 06:35:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-foreach-to-extract-value-from-another-json-object/m-p/642098#M222425</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-05-03T06:35:53Z</dc:date>
    </item>
  </channel>
</rss>

