<?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 parse a JSON with a mutable node? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558694#M158735</link>
    <description>&lt;P&gt;If record is the last field in root, you can edit it to convert the single instance to an array. If not, the sed will need to be adjusted accordingly&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval event=split("{
  \"root\": {
    \"metadata\": {
      \"name\": \"Jay Doe\",
      \"email\": \"jay.doe@example.com\"
    },
    \"record\": {
      \"row\": {
        \"source_ip\": \"8.8.8.8\",
        \"count\": \"1\"
      },
      \"identifiers\": {
        \"to\": \"companyfoo.com\",
        \"from\": \"example.com\",
        \"header_from\": \"example.com\"
      }
    }
  }
}|{
    \"root\": {
        \"metadata\": {
            \"name\": \"Bob Doe\",
            \"email\": \"bob.doe@example.com\"
        },
        \"record\": [
            {
                \"row\": {
                    \"source_ip\": \"8.8.8.8\",
                    \"count\": \"1\"
                },
                \"identifiers\": {
                    \"to\": \"companyfoo.com\",
                    \"from\": \"example.com\",
                    \"header_from\": \"example.com\"
                }
            },
            {
                \"row\": {
                    \"source_ip\": \"8.8.4.4\",
                    \"count\": \"5\"
                },
                \"identifiers\": {
                    \"to\": \"companybar.com\",
                    \"from\": \"example.com\",
                    \"header_from\": \"example.com\"
                }
            }
        ]
    }
}","|")
| mvexpand event


| rex field=event mode=sed "s/(?s)(\"record\":\s)\{(.*)(\})(\s+\}$)/\1[{\2]\3\4/g"
| spath input=event path=root.record{} output=record
| spath input=event path=root.metadata output=metadata
| spath input=metadata
| mvexpand record
| spath input=record path=row output=row
| spath input=row
| spath input=record path=identifiers output=identifiers
| spath input=identifiers
| table name email source_ip count to from header_from&lt;/LI-CODE&gt;&lt;P&gt;The part before the blank lines just sets up some test data with your two sample events&lt;/P&gt;</description>
    <pubDate>Thu, 08 Jul 2021 14:01:59 GMT</pubDate>
    <dc:creator>ITWhisperer</dc:creator>
    <dc:date>2021-07-08T14:01:59Z</dc:date>
    <item>
      <title>How to parse a JSON with a mutable node?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558685#M158727</link>
      <description>&lt;P&gt;I receive some logs in json format, but one of the nodes is mutable, sometimes it's an array, sometimes it is not. Take for example the two possible logs below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Single record:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{
  "root": {
    "metadata": {
      "name": "Jay Doe",
      "email": "jay.doe@example.com"
    },
    "record": {
      "row": {
        "source_ip": "8.8.8.8",
        "count": "1"
      },
      "identifiers": {
        "to": "companyfoo.com",
        "from": "example.com",
        "header_from": "example.com"
      }
    }
  }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Multiple records:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{
    "root": {
        "metadata": {
            "name": "Bob Doe",
            "email": "bob.doe@example.com"
        },
        "record": [
            {
                "row": {
                    "source_ip": "8.8.8.8",
                    "count": "1"
                },
                "identifiers": {
                    "to": "companyfoo.com",
                    "from": "example.com",
                    "header_from": "example.com"
                }
            },
            {
                "row": {
                    "source_ip": "8.8.4.4",
                    "count": "5"
                },
                "identifiers": {
                    "to": "companybar.com",
                    "from": "example.com",
                    "header_from": "example.com"
                }
            }
        ]
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only part that is mutable is &lt;STRONG&gt;root.record&lt;/STRONG&gt;. I want to be able to parse both formats and have a table like so:&lt;/P&gt;&lt;TABLE border="1" width="100.00000000000001%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="14.285714285714286%"&gt;name&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;email&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;source_ip&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;count&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;to&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;from&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;header_from&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="14.285714285714286%"&gt;Jay Doe&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;jay.doe@exampel.com&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;8.8.8.8&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;1&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;companyfoo.com&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;example.com&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;example.com&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="14.285714285714286%"&gt;Bob Doe&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;bob.doe@example.com&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;8.8.8.8&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;1&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;companyfoo.com&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;example.com&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;example.com&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="14.285714285714286%"&gt;Bob Doe&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;bob.doe@example.com&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;8.8.4.4&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;5&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;companybar.com&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;example.com&lt;/TD&gt;&lt;TD width="14.285714285714286%"&gt;example.com&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible without using heavy and/or complex queries?&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 13:23:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558685#M158727</guid>
      <dc:creator>JChris_</dc:creator>
      <dc:date>2021-07-08T13:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a JSON with a mutable node?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558692#M158733</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/234030"&gt;@JChris_&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you try this,&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;your_search_goes_here&amp;gt;
| spath input=_raw path=root.record{} output=array_items 
| spath input=_raw path=root.metadata.name output=name 
| spath input=_raw path=root.metadata.email output=email 
| mvexpand array_items 
| spath input=array_items path=row output=row 
| spath input=array_items path=identifiers output=id 
| spath input=row 
| spath input=id 
| table name email count source_ip to from header_from&lt;/LI-CODE&gt;&lt;P&gt;Output for the second sample provided,&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="venkatasri_0-1625752468087.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/14986i4D35FE89DCAF5ADA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="venkatasri_0-1625752468087.png" alt="venkatasri_0-1625752468087.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;---&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;An upvote would be appreciated and Accept solution if this reply helps!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 13:55:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558692#M158733</guid>
      <dc:creator>venkatasri</dc:creator>
      <dc:date>2021-07-08T13:55:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a JSON with a mutable node?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558693#M158734</link>
      <description>&lt;P&gt;This only works for root.record when it is an array. The query should work for both.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 14:00:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558693#M158734</guid>
      <dc:creator>JChris_</dc:creator>
      <dc:date>2021-07-08T14:00:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a JSON with a mutable node?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558694#M158735</link>
      <description>&lt;P&gt;If record is the last field in root, you can edit it to convert the single instance to an array. If not, the sed will need to be adjusted accordingly&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval event=split("{
  \"root\": {
    \"metadata\": {
      \"name\": \"Jay Doe\",
      \"email\": \"jay.doe@example.com\"
    },
    \"record\": {
      \"row\": {
        \"source_ip\": \"8.8.8.8\",
        \"count\": \"1\"
      },
      \"identifiers\": {
        \"to\": \"companyfoo.com\",
        \"from\": \"example.com\",
        \"header_from\": \"example.com\"
      }
    }
  }
}|{
    \"root\": {
        \"metadata\": {
            \"name\": \"Bob Doe\",
            \"email\": \"bob.doe@example.com\"
        },
        \"record\": [
            {
                \"row\": {
                    \"source_ip\": \"8.8.8.8\",
                    \"count\": \"1\"
                },
                \"identifiers\": {
                    \"to\": \"companyfoo.com\",
                    \"from\": \"example.com\",
                    \"header_from\": \"example.com\"
                }
            },
            {
                \"row\": {
                    \"source_ip\": \"8.8.4.4\",
                    \"count\": \"5\"
                },
                \"identifiers\": {
                    \"to\": \"companybar.com\",
                    \"from\": \"example.com\",
                    \"header_from\": \"example.com\"
                }
            }
        ]
    }
}","|")
| mvexpand event


| rex field=event mode=sed "s/(?s)(\"record\":\s)\{(.*)(\})(\s+\}$)/\1[{\2]\3\4/g"
| spath input=event path=root.record{} output=record
| spath input=event path=root.metadata output=metadata
| spath input=metadata
| mvexpand record
| spath input=record path=row output=row
| spath input=row
| spath input=record path=identifiers output=identifiers
| spath input=identifiers
| table name email source_ip count to from header_from&lt;/LI-CODE&gt;&lt;P&gt;The part before the blank lines just sets up some test data with your two sample events&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 14:01:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558694#M158735</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2021-07-08T14:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a JSON with a mutable node?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558738#M158747</link>
      <description>&lt;P&gt;Thanks! This worked, but I end up changing the source so that ALL root.record are sent as list, even if there is only one record. This makes it easier and more consistent.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 17:50:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558738#M158747</guid>
      <dc:creator>JChris_</dc:creator>
      <dc:date>2021-07-08T17:50:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse a JSON with a mutable node?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558739#M158748</link>
      <description>&lt;P&gt;Changing the source is the better approach.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 17:59:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-parse-a-JSON-with-a-mutable-node/m-p/558739#M158748</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2021-07-08T17:59:04Z</dc:date>
    </item>
  </channel>
</rss>

