<?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: Check multiple arrays for value without knowing parent node(s) in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Check-multiple-arrays-for-value-without-knowing-parent-node-s/m-p/704729#M238740</link>
    <description>&lt;P&gt;Something like this?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| spath
| foreach *.ReportTags*
    [| eval fields=if(isnotnull(mvfind('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;',"Tag1")), if(isnull(fields),"&amp;lt;&amp;lt;MATCHSEG1&amp;gt;&amp;gt;",mvappend(fields,"&amp;lt;&amp;lt;MATCHSEG1&amp;gt;&amp;gt;")), fields)]&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 19 Nov 2024 15:39:45 GMT</pubDate>
    <dc:creator>ITWhisperer</dc:creator>
    <dc:date>2024-11-19T15:39:45Z</dc:date>
    <item>
      <title>Check multiple arrays for value without knowing parent node(s)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Check-multiple-arrays-for-value-without-knowing-parent-node-s/m-p/704725#M238739</link>
      <description>&lt;P&gt;The structure of JSON in my log events is roughly as follows&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{
    "Info": {
        "Apps": {
            "ReportingServices": {
                "ReportTags": [
                    "Tag1"
                ],
                "UserTags": [
                    "Tag2",
                    "Tag3"
                ]
            },
            "MessageQueue": {
                "ReportTags": [
                    "Tag1",
                    "Tag4"
                ],
                "UserTags": [
                    "Tag3",
                    "Tag4",
                    "Tag5"
                ]
            },
            "Frontend": {
                "ClientTags": [
                    "Tag12",
                    "Tag47"
                ]
            }
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The number of fields in "Apps" is unknown, as are their names. Given this structure I need to check if a given tag ("Tag1", "Tag2", ...) exists in in a given array ("ReportTags", "UserTags", [..]), regardless of parent. If it does, I need the distinct names of parent field names that contain this.&lt;BR /&gt;Example 1: The input to the query is "ReportTags" and&amp;nbsp;"Tag1". I'd expect it to output both "&lt;SPAN&gt;ReportingServices&lt;/SPAN&gt;" and "&lt;SPAN&gt;MessageQueue&lt;/SPAN&gt;" because both of them contain a "ReportTags" array that contains "Tag1".&lt;BR /&gt;Example 2: The input to the query is "UserTags" and&amp;nbsp;"Tag5". I'd expect it to output only "&lt;SPAN&gt;MessageQueue&lt;/SPAN&gt;" because only this one contains a "UserTags" array that contains this "Tag5".&lt;BR /&gt;&lt;BR /&gt;I have looked at various questions on this forum, tried various combinations of mvexpand and such but I have not been able to write a query that does exactly this. Any hints and/or help would be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Nov 2024 15:25:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Check-multiple-arrays-for-value-without-knowing-parent-node-s/m-p/704725#M238739</guid>
      <dc:creator>mrsampson</dc:creator>
      <dc:date>2024-11-19T15:25:13Z</dc:date>
    </item>
    <item>
      <title>Re: Check multiple arrays for value without knowing parent node(s)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Check-multiple-arrays-for-value-without-knowing-parent-node-s/m-p/704729#M238740</link>
      <description>&lt;P&gt;Something like this?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| spath
| foreach *.ReportTags*
    [| eval fields=if(isnotnull(mvfind('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;',"Tag1")), if(isnull(fields),"&amp;lt;&amp;lt;MATCHSEG1&amp;gt;&amp;gt;",mvappend(fields,"&amp;lt;&amp;lt;MATCHSEG1&amp;gt;&amp;gt;")), fields)]&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 19 Nov 2024 15:39:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Check-multiple-arrays-for-value-without-knowing-parent-node-s/m-p/704729#M238740</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2024-11-19T15:39:45Z</dc:date>
    </item>
    <item>
      <title>Re: Check multiple arrays for value without knowing parent node(s)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Check-multiple-arrays-for-value-without-knowing-parent-node-s/m-p/704793#M238747</link>
      <description>&lt;P&gt;Small improvements.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;The wildcard should apply to &amp;lt;anything&amp;gt;Tags{}.&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/MultivalueEvalFunctions#mvfind.28.26lt.3Bmv.26gt.3B.2C.26lt.3Bregex.26gt.3B.29" target="_blank" rel="noopener"&gt;mvfind&lt;/A&gt;&amp;nbsp;uses regex. &amp;nbsp;If you need string match, there is too much work to convert an arbitrary string into regex. &amp;nbsp;But Splunk's equality operator applies in multivalue context.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;So, using foreach suggested by&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;, you can do&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| foreach *Tags{}
    [| eval fields=mvappend(fields, if('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;' == "Tag4", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your sample data will give&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV class=""&gt;fields&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.MessageQueue.UserTags{}&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Since 8.2, Splunk introduced a set of JSON functions. &amp;nbsp;You can actually use a more formal, semantic approach, although the algorithm is messier because iteration capabilities are limited in SPL. (It is also limited as SPL doesn't support recursion.) Here is an illustration.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval key = json_array_to_mv(json_keys(_raw))
| mvexpand key
| eval key1 = json_array_to_mv(json_keys(json_extract(_raw, key)))
| mvexpand key1
| eval key = if(isnull(key1), key, key . "." . key1)
| eval key1 = json_array_to_mv(json_keys(json_extract(_raw, key)))
| mvexpand key1
| eval key = if(isnull(key1), key, key . "." . key1)
| eval key1 = json_array_to_mv(json_keys(json_extract(_raw, key)))
| mvexpand key1
| eval key = if(isnull(key1), key, key . "." . key1)
| eval key1 = json_array_to_mv(json_keys(json_extract(_raw, key)))
| eval key = if(isnull(key1), key, key . "." . key1)
| eval value = json_array_to_mv(json_extract(_raw, key))
| where value == "Tag4"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The above code assumes a path depth of 5 even though your data only has depth of 4. &amp;nbsp;The result is&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;key&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;value&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Info.Apps.MessageQueue.ReportTags&lt;/TD&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;TR&gt;&lt;TD&gt;Info.Apps.MessageQueue.UserTags&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;Tag3&lt;/DIV&gt;&lt;DIV class=""&gt;Tag4&lt;/DIV&gt;&lt;DIV class=""&gt;Tag5&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Here is an emulation you can play with and compare with real data&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\"
                ]
            }
        }
    }
}"
| fields - _time
| spath
``` data emulation above ```&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2024 06:29:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Check-multiple-arrays-for-value-without-knowing-parent-node-s/m-p/704793#M238747</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2024-11-20T06:29:12Z</dc:date>
    </item>
  </channel>
</rss>

