<?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: how to search in array inside a foreach loop in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708940#M239656</link>
    <description>&lt;P&gt;I see you want to determine full paths of the value input list. &amp;nbsp;You have a second requirement that the input be a JSON array, &amp;nbsp;&lt;SPAN&gt;&lt;FONT face="andale mono,times"&gt;["Tag3", "Tag4"]&lt;/FONT&gt;,&amp;nbsp;&lt;/SPAN&gt;and a third that the code needs to run in 8.0, which precludes JSON functions introduced in 8.1. &amp;nbsp;Note&amp;nbsp;each of the path{} array has multiple values. &amp;nbsp;Without help of JSON functions, you need to handle that first.&lt;/P&gt;&lt;P&gt;The most common way to do this is with&amp;nbsp;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Mvexpand" target="_blank" rel="noopener"&gt;mvexpand&lt;/A&gt;. (The input array also needs this.)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval _raw = "{
    \"Info\": {
        \"Apps\": {
            \"ReportingServices\": {
                \"ReportTags\": [
                    \"Tag1\"
                ],
                \"UserTags\": [
                    \"Tag2\",
                    \"Tag3\"
                ]
            },
            \"MessageQueue\": {
                \"ReportTags\": [
                    \"Tag1\",
                    \"Tag4\"
                ],
                \"UserTags\": [
                    \"Tag3\",
                    \"Tag4\",
                    \"Tag5\"
                ]
            },
            \"Frontend\": {
                \"ClientTags\": [
                    \"Tag12\",
                    \"Tag47\"
                ]
            }
        }
    }
}"
| spath
``` data emulation above ```
| eval Tags = "[\"Tag3\", \"Tag4\"]"
| foreach *Tags{}
    [mvexpand &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;]
| spath input=Tags
| mvexpand {}
| foreach *Tags{}
    [eval tags=mvappend(tags, if(lower('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') = lower('{}'), "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]
| dedup tags
| stats values(tags)&lt;/LI-CODE&gt;&lt;P&gt;If your dataset is large, mvexpand has some limitations.&lt;/P&gt;</description>
    <pubDate>Thu, 16 Jan 2025 04:41:12 GMT</pubDate>
    <dc:creator>yuanliu</dc:creator>
    <dc:date>2025-01-16T04:41:12Z</dc:date>
    <item>
      <title>how to search in array inside a foreach loop</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708780#M239637</link>
      <description>&lt;P&gt;This is an example of the structure of my data and the query I am currently using. I have tried around 10 different solutions based on various examples from stackoverflow.com and&amp;nbsp; community.splunk.com. But I have not figured out how to change this query such that &lt;STRONG&gt;eval Tag = "Tag1"&lt;/STRONG&gt; can become an array eval &lt;STRONG&gt;Tags = ["Tag1", "Tag4"]&lt;/STRONG&gt; and I will get entries for all tags that exist in the array. Could someone guide me in the right direction?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval _raw = "{
    \"Info\": {
        \"Apps\": {
            \"ReportingServices\": {
                \"ReportTags\": [
                    \"Tag1\"
                ],
                \"UserTags\": [
                    \"Tag2\",
                    \"Tag3\"
                ]
            },
            \"MessageQueue\": {
                \"ReportTags\": [
                    \"Tag1\",
                    \"Tag4\"
                ],
                \"UserTags\": [
                    \"Tag3\",
                    \"Tag4\",
                    \"Tag5\"
                ]
            },
            \"Frontend\": {
                \"ClientTags\": [
                    \"Tag12\",
                    \"Tag47\"
                ]
            }
        }
    }
}"
| eval Tag = "Tag1"
| spath
| foreach *ReportTags{}
    [| eval tags=mvappend(tags, if(lower('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') = lower(Tag), "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]
| dedup tags
| stats values(tags)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2025 18:22:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708780#M239637</guid>
      <dc:creator>mrsampson</dc:creator>
      <dc:date>2025-01-14T18:22:21Z</dc:date>
    </item>
    <item>
      <title>Re: how to search in array inside a foreach loop</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708795#M239640</link>
      <description>&lt;P&gt;What does your expected output look like?&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2025 19:08:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708795#M239640</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2025-01-14T19:08:08Z</dc:date>
    </item>
    <item>
      <title>Re: how to search in array inside a foreach loop</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708851#M239642</link>
      <description>&lt;P&gt;Do you mean this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval _raw = "{
    \"Info\": {
        \"Apps\": {
            \"ReportingServices\": {
                \"ReportTags\": [
                    \"Tag1\"
                ],
                \"UserTags\": [
                    \"Tag2\",
                    \"Tag3\"
                ]
            },
            \"MessageQueue\": {
                \"ReportTags\": [
                    \"Tag1\",
                    \"Tag4\"
                ],
                \"UserTags\": [
                    \"Tag3\",
                    \"Tag4\",
                    \"Tag5\"
                ]
            },
            \"Frontend\": {
                \"ClientTags\": [
                    \"Tag12\",
                    \"Tag47\"
                ]
            }
        }
    }
}"
| eval Tag = "Tag1"
| spath
| foreach *ReportTags{}
    [| eval tags=mvappend(tags, if(lower('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') = lower(Tag), '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', null()))]
| dedup tags
| stats values(tags)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This gives&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV class=""&gt;values(tags)&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV class=""&gt;Tag1&lt;/DIV&gt;&lt;DIV class=""&gt;Tag4&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Note when you use double quote on the right-hand side of an eval expression, in quoted entity is used as literal, therefore your original search gives&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV class=""&gt;values(tags)&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV class=""&gt;Info.Apps.MessageQueue.ReportTags{}&lt;/DIV&gt;&lt;DIV class=""&gt;Info.Apps.ReportingServices.ReportTags{}&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 15 Jan 2025 08:34:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708851#M239642</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2025-01-15T08:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: how to search in array inside a foreach loop</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708856#M239643</link>
      <description>&lt;P&gt;for this&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval Tags = ["Tag3", "Tag4]
| spath
| foreach *Tags{}
    [| eval tags=mvappend(tags, if(lower('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') = lower(Tag), "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]
| dedup tags
| stats values(tags)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I would like to get&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Info.Apps.MessageQueue.ReportTags{}
Info.Apps.ReportingServices.ReportTags{}
Info.Apps.MessageQueue.UserTags{}​&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 10:19:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708856#M239643</guid>
      <dc:creator>mrsampson</dc:creator>
      <dc:date>2025-01-15T10:19:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to search in array inside a foreach loop</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708857#M239644</link>
      <description>&lt;P&gt;not really, the main point in here is that my input to this query, instead of a simple value would be an array. e.g. &lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;current input format:&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;| eval Tag = "Tag1"&lt;/LI-CODE&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;desired input format:&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;| eval Tags = ["Tag3", "Tag4"]&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 15 Jan 2025 15:08:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708857#M239644</guid>
      <dc:creator>mrsampson</dc:creator>
      <dc:date>2025-01-15T15:08:48Z</dc:date>
    </item>
    <item>
      <title>Re: how to search in array inside a foreach loop</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708861#M239646</link>
      <description>&lt;P&gt;Splunk's version of arrays is multivalue field, so if you change you input to a multivalue field, you could do something like this&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval Tag = split(lower("Tag3,Tag4"),",")
| spath
| foreach *Tags{}
    [| eval field="&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"
    | foreach &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; mode=multivalue
        [| eval tags=if(isnull(tags),if(mvfind(Tag,lower('&amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;')) &amp;gt;= 0, field, null()),mvappend(tags, if(mvfind(Tag,lower('&amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;')) &amp;gt;= 0, field, null())))]
    ]
| stats values(tags)&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 15 Jan 2025 11:23:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708861#M239646</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2025-01-15T11:23:27Z</dc:date>
    </item>
    <item>
      <title>Re: how to search in array inside a foreach loop</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708895#M239647</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Splunk's version of arrays is multivalue field, so if you change you input to a multivalue field, you could do something like this&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval Tag = split(lower("Tag3,Tag4"),",")
| spath
| foreach *Tags{}
    [| eval field="&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"
    | foreach &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; mode=multivalue
        [| eval tags=if(isnull(tags),if(mvfind(Tag,lower('&amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;')) &amp;gt;= 0, field, null()),mvappend(tags, if(mvfind(Tag,lower('&amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;')) &amp;gt;= 0, field, null())))]
    ]
| stats values(tags)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;Thank you for your response and the example, currently it is returnin 0 results for me. Could it have something to do with my Splunk version? I a&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 13:43:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708895#M239647</guid>
      <dc:creator>mrsampson</dc:creator>
      <dc:date>2025-01-15T13:43:00Z</dc:date>
    </item>
    <item>
      <title>Re: how to search in array inside a foreach loop</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708896#M239648</link>
      <description>&lt;P&gt;m using 8.0.5&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 13:43:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708896#M239648</guid>
      <dc:creator>mrsampson</dc:creator>
      <dc:date>2025-01-15T13:43:42Z</dc:date>
    </item>
    <item>
      <title>Re: how to search in array inside a foreach loop</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708897#M239649</link>
      <description>&lt;P&gt;Yes, foreach mode=multivalue appeared in 9.0.0.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 14:07:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708897#M239649</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2025-01-15T14:07:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to search in array inside a foreach loop</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708920#M239653</link>
      <description>&lt;P&gt;Does anyone know how to do this on Splunk v8.0.5?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 18:19:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708920#M239653</guid>
      <dc:creator>mrsampson</dc:creator>
      <dc:date>2025-01-15T18:19:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to search in array inside a foreach loop</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708934#M239655</link>
      <description>&lt;P&gt;Try something like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval Tag = split("Tag3,Tag4",",")
| mvexpand Tag
| spath
| foreach *Tags{}
    [| eval tags=if(mvfind(lower('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'), "^".lower(Tag)."$") &amp;gt;= 0,mvappend(tags, "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"), tags)]
| stats values(tags)&lt;/LI-CODE&gt;&lt;P&gt;Note that mvfind uses regex so you may get some odd results if your tags have special characters in them&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 23:56:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708934#M239655</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2025-01-15T23:56:52Z</dc:date>
    </item>
    <item>
      <title>Re: how to search in array inside a foreach loop</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708940#M239656</link>
      <description>&lt;P&gt;I see you want to determine full paths of the value input list. &amp;nbsp;You have a second requirement that the input be a JSON array, &amp;nbsp;&lt;SPAN&gt;&lt;FONT face="andale mono,times"&gt;["Tag3", "Tag4"]&lt;/FONT&gt;,&amp;nbsp;&lt;/SPAN&gt;and a third that the code needs to run in 8.0, which precludes JSON functions introduced in 8.1. &amp;nbsp;Note&amp;nbsp;each of the path{} array has multiple values. &amp;nbsp;Without help of JSON functions, you need to handle that first.&lt;/P&gt;&lt;P&gt;The most common way to do this is with&amp;nbsp;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Mvexpand" target="_blank" rel="noopener"&gt;mvexpand&lt;/A&gt;. (The input array also needs this.)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval _raw = "{
    \"Info\": {
        \"Apps\": {
            \"ReportingServices\": {
                \"ReportTags\": [
                    \"Tag1\"
                ],
                \"UserTags\": [
                    \"Tag2\",
                    \"Tag3\"
                ]
            },
            \"MessageQueue\": {
                \"ReportTags\": [
                    \"Tag1\",
                    \"Tag4\"
                ],
                \"UserTags\": [
                    \"Tag3\",
                    \"Tag4\",
                    \"Tag5\"
                ]
            },
            \"Frontend\": {
                \"ClientTags\": [
                    \"Tag12\",
                    \"Tag47\"
                ]
            }
        }
    }
}"
| spath
``` data emulation above ```
| eval Tags = "[\"Tag3\", \"Tag4\"]"
| foreach *Tags{}
    [mvexpand &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;]
| spath input=Tags
| mvexpand {}
| foreach *Tags{}
    [eval tags=mvappend(tags, if(lower('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') = lower('{}'), "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]
| dedup tags
| stats values(tags)&lt;/LI-CODE&gt;&lt;P&gt;If your dataset is large, mvexpand has some limitations.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2025 04:41:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-search-in-array-inside-a-foreach-loop/m-p/708940#M239656</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2025-01-16T04:41:12Z</dc:date>
    </item>
  </channel>
</rss>

