<?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: Json Parsing.. in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Json-Parsing/m-p/674821#M230978</link>
    <description>&lt;P&gt;1. This has nothing to do (or at least not much) with parsing, more about summarizing data from your event.&lt;/P&gt;&lt;P&gt;2. Splunk seems to have problems with using spath when the names contain dots, so extracting the "a.com" part and splitting it might not be that easy.&lt;/P&gt;</description>
    <pubDate>Fri, 19 Jan 2024 10:33:34 GMT</pubDate>
    <dc:creator>PickleRick</dc:creator>
    <dc:date>2024-01-19T10:33:34Z</dc:date>
    <item>
      <title>Json Parsing..</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Json-Parsing/m-p/674805#M230977</link>
      <description>&lt;P&gt;Json :-&lt;/P&gt;&lt;P&gt;| makeresults | eval _raw="{&lt;BR /&gt;\"a.com\": [&lt;BR /&gt;{ \"yahoo.com\":\"10ms\",\"trans-id\": \"x1\"},&lt;BR /&gt;{ \"google.com\":\"20ms\",\"trans-id\": \"x2\"}&lt;BR /&gt;],&lt;BR /&gt;\"trans-id\":\"m1\",&lt;BR /&gt;\"duration\":\"33ms\"&lt;BR /&gt;}"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;need output in below format:-&lt;/P&gt;&lt;TABLE border="0" width="448" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="64" height="19"&gt;_time&lt;/TD&gt;&lt;TD width="64"&gt;Trans_id&lt;/TD&gt;&lt;TD width="64"&gt;url&lt;/TD&gt;&lt;TD width="64"&gt;Duration&lt;/TD&gt;&lt;TD width="64"&gt;sub_duration&lt;/TD&gt;&lt;TD width="64"&gt;sub_url&lt;/TD&gt;&lt;TD width="64"&gt;sub_trans_id&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="19"&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;m1&lt;/TD&gt;&lt;TD&gt;a.com&lt;/TD&gt;&lt;TD&gt;33ms&lt;/TD&gt;&lt;TD&gt;10ms&lt;/TD&gt;&lt;TD&gt;yahoo.com&lt;/TD&gt;&lt;TD&gt;x1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="19"&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;m1&lt;/TD&gt;&lt;TD&gt;a.com&lt;/TD&gt;&lt;TD&gt;33ms&lt;/TD&gt;&lt;TD&gt;20ms&lt;/TD&gt;&lt;TD&gt;google.com&lt;/TD&gt;&lt;TD&gt;x2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 19 Jan 2024 09:46:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Json-Parsing/m-p/674805#M230977</guid>
      <dc:creator>onthakur</dc:creator>
      <dc:date>2024-01-19T09:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: Json Parsing..</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Json-Parsing/m-p/674821#M230978</link>
      <description>&lt;P&gt;1. This has nothing to do (or at least not much) with parsing, more about summarizing data from your event.&lt;/P&gt;&lt;P&gt;2. Splunk seems to have problems with using spath when the names contain dots, so extracting the "a.com" part and splitting it might not be that easy.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2024 10:33:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Json-Parsing/m-p/674821#M230978</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2024-01-19T10:33:34Z</dc:date>
    </item>
    <item>
      <title>Re: Json Parsing..</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Json-Parsing/m-p/674847#M230981</link>
      <description>&lt;P&gt;SPL is a bit wonky but got results in the final format you were looking for, I'm curious how this SPL will perform against your live data.&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;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults | eval _raw="{
\"a.com\": [
{ \"yahoo.com\":\"10ms\",\"trans-id\": \"x1\"},
{ \"google.com\":\"20ms\",\"trans-id\": \"x2\"}
],
\"b.com\": [
{ \"aspera.com\":\"30ms\",\"trans-id\": \"x3\"},
{ \"arista.com\":\"40ms\",\"trans-id\": \"x4\"}
],
\"trans-id\":\"m1\",
\"duration\":\"33ms\"
}"
    
    ``` start parsing json object ```
    | fromjson _raw
    | foreach *.*
        [
            | eval
                url_json=mvappend(
                    url_json,
                        case(
                            mvcount('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;')==1, if(isnotnull(json('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;')), json_set('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', "url", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"), null()),
                            mvcount('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;')&amp;gt;1, mvmap('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', if(isnotnull(json('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;')), json_set('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', "url", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"), null()))
                            )
                    )
            ]
    | fields + _time, url_json, "trans-id", duration
    | rename
        "trans-id" as "top_trans-id"
    | fields - _raw
    | mvexpand url_json
    | fromjson url_json
    | fields - url_json
    | foreach *.*
        [
            | eval
                sub_url=if(
                    isnotnull('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') AND isnull(sub_url),
                        "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;",
                        'sub_url'
                    ),
                sub_duration=if(
                    isnotnull('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') AND isnull(sub_duration),
                        '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;',
                        'sub_duration'
                    )
            ]
    | rename
        "trans-id" as "sub_trans-id"
    | fields + _time, "top_trans-id", url, duration, sub_duration, sub_url, sub_trans-id
    | rename
        "top_trans-id" as "trans-id"&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;Final output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dtburrows3_0-1705678536919.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/29034i2F8BBC477A8E2495/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dtburrows3_0-1705678536919.png" alt="dtburrows3_0-1705678536919.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are some pretty big assumptions here, biggest being that the keys of the _raw json will have fields with the "*.*" format or a dot in the fieldname (domain names)&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2024 15:35:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Json-Parsing/m-p/674847#M230981</guid>
      <dc:creator>dtburrows3</dc:creator>
      <dc:date>2024-01-19T15:35:46Z</dc:date>
    </item>
    <item>
      <title>Re: Json Parsing..</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Json-Parsing/m-p/674902#M230990</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/231884"&gt;@PickleRick&lt;/a&gt;&amp;nbsp;said, this is not about parsing but about presentation, and that spath command that we usually use is not handling JSON keys containing dot (.) correctly because in SPL, as well as in many other languages that flatten structured data use dot to represent hierarchy.&lt;/P&gt;&lt;P&gt;But keys containing dot are not the only problem that makes&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/263242"&gt;@dtburrows3&lt;/a&gt;'s solution so wonky. &amp;nbsp;The bigger problem is the data design. &amp;nbsp;It uses implied semantics about what represents a URL. &amp;nbsp;Implied semantics in structured data is generally unacceptable. (At higher level, this is abusing key names to represent data.) &amp;nbsp;If you have any influence with your developers, beg them to change data structure so key and data are completely separate.&lt;/P&gt;&lt;P&gt;Thanks to&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/263242"&gt;@dtburrows3&lt;/a&gt;, I learned that &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Fromjson" target="_blank" rel="noopener"&gt;fromjson&lt;/A&gt; (introduced in 9.0) is more robust than spath (from 7.0 or earlier), and learned the trick to leverage the evil dot in key name in order to single out actual data in abused structure, namely &lt;FONT face="courier new,courier"&gt;foreach *.*&lt;/FONT&gt;. &amp;nbsp;It is less robust but works for the limited dataset. &amp;nbsp;So, I offer a more semantic, hopefully less wonky solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults | eval _raw="{
\"a.com\": [
{ \"yahoo.com\":\"10ms\",\"trans-id\": \"x1\"},
{ \"google.com\":\"20ms\",\"trans-id\": \"x2\"}
],
\"trans-id\":\"m1\",
\"duration\":\"33ms\"
}"
``` data emulation above ```
| table _*
| fromjson _raw
| rename duration as Duration, trans-id as Trans_id
| foreach *
    [eval url = mvappend(url, if("&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;" IN ("Duration", "Trans_id"), null, "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"))]
| mvexpand url ``` nothing in this data structure prevents multiple URLs ```
| foreach *.*
    [mvexpand &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;
    | eval subkey = json_array_to_mv(json_keys('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'))
    | eval sub_trans_id = json_extract('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', "trans-id")
    | eval subdata = json_object()
    | eval subdata = mvmap(subkey, if(subkey == "trans-id", null(), json_set(subdata,
      "sub_url", subkey, "sub_duration", json_extract_exact('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', subkey))))]
| fromjson subdata
| table _time Trans_id url Duration sub_duration sub_url sub_trans_id&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output is&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;_time&lt;/TD&gt;&lt;TD&gt;Trans_id&lt;/TD&gt;&lt;TD&gt;url&lt;/TD&gt;&lt;TD&gt;Duration&lt;/TD&gt;&lt;TD&gt;sub_duration&lt;/TD&gt;&lt;TD&gt;sub_url&lt;/TD&gt;&lt;TD&gt;sub_trans_id&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2024-01-19 13:01:54&lt;/TD&gt;&lt;TD&gt;m1&lt;/TD&gt;&lt;TD&gt;a.com&lt;/TD&gt;&lt;TD&gt;33ms&lt;/TD&gt;&lt;TD&gt;10ms&lt;/TD&gt;&lt;TD&gt;yahoo.com&lt;/TD&gt;&lt;TD&gt;x1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2024-01-19 13:01:54&lt;/TD&gt;&lt;TD&gt;m1&lt;/TD&gt;&lt;TD&gt;a.com&lt;/TD&gt;&lt;TD&gt;33ms&lt;/TD&gt;&lt;TD&gt;20ms&lt;/TD&gt;&lt;TD&gt;google.com&lt;/TD&gt;&lt;TD&gt;x2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 19 Jan 2024 21:47:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Json-Parsing/m-p/674902#M230990</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2024-01-19T21:47:21Z</dc:date>
    </item>
  </channel>
</rss>

