<?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 convert nested xml block into json formatted nested block ? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644567#M223230</link>
    <description>&lt;P&gt;After spending hours doing trial and error, I finally was able to craft an SPL that gave me the expected result&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=botsv3 sourcetype=xmlwineventlog
| spath
| rename Event.EventData.Data as EventDataData, Event.EventData.Data{@Name} as EventDataName EventID as ID Computer as Hostname TimeCreated as Timestamp
| eval EventData=json_object("EventData",json_object(mvindex(EventDataName,0),mvindex(EventDataData,0)))
| eval count=0
| foreach mode=multivalue EventDataName
    [
| eval EventData=json_set(EventData,"EventData."+mvindex(EventDataName,mvcount(&amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;)+count),mvindex(EventDataData,mvcount(&amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;)+count)),count=count+1]
| foreach ID Hostname Timestamp
    [
| eval EventData=json_set(EventData,"&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;",&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;)]
| table EventData&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 25 May 2023 00:24:56 GMT</pubDate>
    <dc:creator>damode1</dc:creator>
    <dc:date>2023-05-25T00:24:56Z</dc:date>
    <item>
      <title>How to convert nested xml block into json formatted nested block ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644381#M223190</link>
      <description>&lt;P&gt;I have the below sample botsv3 sample data set which is sysmon in xml format. I need to convert that into json formatted events.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample Current XML format - code block :1&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;&amp;lt;Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'&amp;gt;
    &amp;lt;System&amp;gt;
        &amp;lt;Provider Name='Microsoft-Windows-Sysmon' Guid='{5770385F-C22A-43E0-BF4C-06F5698FFBD9}' /&amp;gt;
        &amp;lt;EventID&amp;gt;1&amp;lt;/EventID&amp;gt;
        &amp;lt;Version&amp;gt;5&amp;lt;/Version&amp;gt;
        &amp;lt;Level&amp;gt;4&amp;lt;/Level&amp;gt;
        &amp;lt;Task&amp;gt;1&amp;lt;/Task&amp;gt;
        &amp;lt;Opcode&amp;gt;0&amp;lt;/Opcode&amp;gt;
        &amp;lt;Keywords&amp;gt;0x8000000000000000&amp;lt;/Keywords&amp;gt;
        &amp;lt;TimeCreated SystemTime='2023-05-21T13:35:45.561534700Z' /&amp;gt;
        &amp;lt;EventRecordID&amp;gt;36885&amp;lt;/EventRecordID&amp;gt;
        &amp;lt;Correlation /&amp;gt;
        &amp;lt;Execution ProcessID='3204' ThreadID='5508' /&amp;gt;
        &amp;lt;Channel&amp;gt;Microsoft-Windows-Sysmon/Operational&amp;lt;/Channel&amp;gt;
        &amp;lt;Computer&amp;gt;BGIST-L.froth.ly&amp;lt;/Computer&amp;gt;
        &amp;lt;Security UserID='S-1-5-18' /&amp;gt;
    &amp;lt;/System&amp;gt;
    &amp;lt;EventData&amp;gt;
        &amp;lt;Data Name='UtcTime'&amp;gt;2023-05-21 15:17:59.931&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='ProcessGuid'&amp;gt;{EBF7A186-1A1C-5B59-0000-0010732E0200}&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='ProcessId'&amp;gt;2684&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='Image'&amp;gt;C:\Windows\System32\svchost.exe&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='FileVersion'&amp;gt;10.0.17134.1 (WinBuild.160101.0800)&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='Description'&amp;gt;Host Process for Windows Services&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='Product'&amp;gt;Microsoft® Windows® Operating System&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='Company'&amp;gt;Microsoft Corporation&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='CommandLine'&amp;gt;c:\windows\system32\svchost.exe -k networkservice -p -s CryptSvc&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='CurrentDirectory'&amp;gt;C:\Windows\system32\&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='User'&amp;gt;NT AUTHORITY\NETWORK SERVICE&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='LogonGuid'&amp;gt;{EBF7A186-1A19-5B59-0000-0020E4030000}&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='LogonId'&amp;gt;0x3e4&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='TerminalSessionId'&amp;gt;0&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='IntegrityLevel'&amp;gt;System&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='Hashes'&amp;gt;
            MD5=32569E403279B3FD2EDB7EBD036273FA,SHA256=C9A28DC8004C3E043CBF8E3A194FDA2B756CE90740DF2175488337281B485F69&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='ParentProcessGuid'&amp;gt;{EBF7A186-1A18-5B59-0000-0010CEA80000}&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='ParentProcessId'&amp;gt;608&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='ParentImage'&amp;gt;C:\Windows\System32\services.exe&amp;lt;/Data&amp;gt;
        &amp;lt;Data Name='ParentCommandLine'&amp;gt;C:\Windows\system32\services.exe&amp;lt;/Data&amp;gt;
    &amp;lt;/EventData&amp;gt;
&amp;lt;/Event&amp;gt;"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expected in JSON format as below. I have only included fields that are actually needed.&amp;nbsp;&lt;STRONG&gt;code block : 2&lt;/STRONG&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;{
    "ID": 1,
    "Timestamp": "2023-05-18T05:07:59.940594300Z",
    "EventData": {
        "FileVersion": "10.0.17134.1 (WinBuild.160101.0800)",
        "Company": "Microsoft Corporation",
        "TerminalSessionId": 0,
        "UtcTime": "2018-08-20 15:18:59.929",
        "Product": "Microsoft® Windows® Operating System",
        "LogonId": "0x3e7",
        "Description": "Find String (QGREP) Utility",
        "OriginalFileName": "findstr.exe",
        "Hashes": "MD5=BCC8F29B929DABF5489C9BE6587FF66D,SHA256=40F83CE0B6E1C894AB766591574ABD5B6780028C874410F2EC224300DF443C81",
        "ParentProcessId": "5428",
        "ParentCommandLine": "C:\\Windows\\system32\\cmd.exe /c netstat -nao | findstr /r \"LISTENING\"",
        "ProcessGuid": "{EBF7A186-0B7B-5B59-0000-001044A3CD01}",
        "ProcessId": "6236",
        "Image": "C:\\Windows\\System32\\findstr.exe",
        "User": "NT AUTHORITY\\SYSTEM",
        "LogonGuid": "{EBF7A186-AB15-5B58-0000-0020E7030000}",
        "LogonGuid": "{EBF7A186-AB15-5B58-0000-0020E7030000}",
        "IntegrityLevel": "System",
        "ParentProcessGuid": "{EBF7A186-0B7B-5B59-0000-0010249FCD01}",
        "ParentImage": "C:\\Windows\\System32\\cmd.exe",
        "RuleName": "",
        "CommandLine": "findstr  /r \"LISTENING\"",
        "CurrentDirectory": "C:\\Windows\\system32\\"
    },
    "Hostname": "BGIST-L.froth.ly",
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The key fields are EventID, Computer and entire EventData block&lt;/P&gt;&lt;P&gt;What I have tried so far&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&amp;nbsp;Used&lt;STRONG&gt; | tojson&lt;/STRONG&gt; command but it didn't created the nested EventData block. It just extracted all field-value pairs as individual objects&lt;/LI&gt;&lt;LI&gt;then tried the below spl but it has fields hardcoded which is not desirable. We want them to be dynamically added to EventData block. The below SPL also led to lot of fields with null values as not all Event IDs had the same fields for obvious reasons.&amp;nbsp;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=main 
| rename EventID as ID Computer as Hostname eventtype as EventType    
| fillnull value=""  
| eval EventData=json_object("FileVersion", FileVersion, "Company", Company, "TerminalSessionId", TerminalSessionId, "UtcTime", UtcTime, "Product", Product,"LogonId",LogonId,"Description",Description,"Hashes",Hashes,"ParentProcessId",ParentProcessId,"ParentCommandLine",ParentCommandLine,"ProcessGuid",ProcessGuid,"ProcessId",ProcessId,"Image",Image,"User",User,"LogonGuid",LogonGuid,"IntegrityLevel",IntegrityLevel,"ParentProcessGuid",ParentProcessGuid,"ParentImage",ParentImage,"CommandLine",CommandLine,"CurrentDirectory",CurrentDirectory),_raw=json_set_exact(json_object(), "ID", ID, "Hostname", Hostname, "EventData", json_extract(EventData))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&amp;nbsp;I tried the below query as well&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=botsv3 sourcetype=xmlwineventlog EventID=1
|  spath
| tojson&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which gave the following result where Eventdata atleast is still in block but both the fields appear as values in &lt;SPAN&gt;&lt;STRONG&gt;Event.EventData.Data&lt;/STRONG&gt; field and values in&amp;nbsp;&lt;STRONG&gt;Event.EventData.Data{@Name}&lt;/STRONG&gt;. I need the values in&amp;nbsp;&lt;STRONG&gt;Event.EventData.Data{@Name} &lt;/STRONG&gt;field&amp;nbsp;to appear as values of corresponding fields which appear as values in the&amp;nbsp;&lt;STRONG&gt;Event.EventData.Data&lt;/STRONG&gt; field and all that as part of EventData nested json block. Basically as shown in&amp;nbsp;&lt;STRONG&gt;code block : 2&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2023-05-24 130814.png" style="width: 839px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/25521iA1F826C481652DD0/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2023-05-24 130814.png" alt="Screenshot 2023-05-24 130814.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help on this would be highly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 May 2023 03:16:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644381#M223190</guid>
      <dc:creator>damode1</dc:creator>
      <dc:date>2023-05-24T03:16:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert nested xml block into json formatted nested block ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644403#M223195</link>
      <description>&lt;P&gt;As I commented in another thread, Splunk doesn't have nested notation. &amp;nbsp;XML and JSON are different enough that SPL has to use two similar but distinct flattened representations. &amp;nbsp;A lot of what you ask have to be crafted by hand, and like in your other post, &amp;nbsp;the key is &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/JSONFunctions#json_object.28.26lt.3Bmembers.26gt.3B.29" target="_blank" rel="noopener"&gt;json_object&lt;/A&gt;. &amp;nbsp;Try this.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rename _raw as temp ``` use this if you want to preserve raw event ```
| eval _raw = mvzip('Event.EventData.Data{@Name}', 'Event.EventData.Data', "=")
| kv
| eval xml2JSON = json_object("ID", 'Event.System.EventID', "Timestamp", 'Event.System.TimeCreated{@SystemTime}',
  "EventData", json_object("FileVersion", FileVersion, "Company", Company, "TerminalSessionId", TerminalSessionId, "UtcTime", UtcTime, "Product", Product,"LogonId",LogonId,"Description",Description,"Hashes",Hashes,"ParentProcessId",ParentProcessId,"ParentCommandLine",ParentCommandLine,"ProcessGuid",ProcessGuid,"ProcessId",ProcessId,"Image",Image,"User",User,"LogonGuid",LogonGuid,"IntegrityLevel",IntegrityLevel,"ParentProcessGuid",ParentProcessGuid,"ParentImage",ParentImage,"CommandLine",CommandLine,"CurrentDirectory",CurrentDirectory),
  "Hostname", 'Event.System.Computer')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 May 2023 07:09:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644403#M223195</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-05-24T07:09:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert nested xml block into json formatted nested block ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644409#M223196</link>
      <description>&lt;P&gt;Your query is almost same as what I tried in my second attempt which I mentioned above. As mentioned there, the problem with that/your query is the field names are hardcoded which also leads to fields with null values.&lt;/P&gt;&lt;P&gt;We need SPL that will dynamically format the event based on what fields are available.&lt;/P&gt;</description>
      <pubDate>Wed, 24 May 2023 07:43:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644409#M223196</guid>
      <dc:creator>damode1</dc:creator>
      <dc:date>2023-05-24T07:43:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert nested xml block into json formatted nested block ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644429#M223198</link>
      <description>&lt;P&gt;If you are prepared to accept some hard-coding of field names, for the Data json you could do something like this&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rename "Event.EventData.Data{@Name}" as name
| rename "Event.EventData.Data" as value
| eval row = mvrange(0,mvcount(name))
| eval json="{".mvjoin(mvmap(row,"\"".mvindex(name,row)."\":\"".trim(mvindex(value,row))."\""),",")."}"&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 24 May 2023 08:49:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644429#M223198</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2023-05-24T08:49:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert nested xml block into json formatted nested block ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644566#M223229</link>
      <description>&lt;P&gt;Thanks for sharing that, however, those fields still appear as individual json objects. I needed those fields nested in EventData json object&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 00:22:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644566#M223229</guid>
      <dc:creator>damode1</dc:creator>
      <dc:date>2023-05-25T00:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert nested xml block into json formatted nested block ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644567#M223230</link>
      <description>&lt;P&gt;After spending hours doing trial and error, I finally was able to craft an SPL that gave me the expected result&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=botsv3 sourcetype=xmlwineventlog
| spath
| rename Event.EventData.Data as EventDataData, Event.EventData.Data{@Name} as EventDataName EventID as ID Computer as Hostname TimeCreated as Timestamp
| eval EventData=json_object("EventData",json_object(mvindex(EventDataName,0),mvindex(EventDataData,0)))
| eval count=0
| foreach mode=multivalue EventDataName
    [
| eval EventData=json_set(EventData,"EventData."+mvindex(EventDataName,mvcount(&amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;)+count),mvindex(EventDataData,mvcount(&amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;)+count)),count=count+1]
| foreach ID Hostname Timestamp
    [
| eval EventData=json_set(EventData,"&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;",&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;)]
| table EventData&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 00:24:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-convert-nested-xml-block-into-json-formatted-nested-block/m-p/644567#M223230</guid>
      <dc:creator>damode1</dc:creator>
      <dc:date>2023-05-25T00:24:56Z</dc:date>
    </item>
  </channel>
</rss>

