<?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 array and stats command in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/JSON-array-and-stats-command/m-p/680910#M232706</link>
    <description>&lt;LI-CODE lang="markup"&gt;| spath _embedded.metadata.data.results{}.notifications output=results
| mvexpand results
| rex field=results "\"(?&amp;lt;ErrorCode&amp;gt;\d+)\":\s*\"(?&amp;lt;ErrorMessage&amp;gt;[^\"]*)\""
| stats count by ErrorCode ErrorMessage&lt;/LI-CODE&gt;</description>
    <pubDate>Sat, 16 Mar 2024 13:45:16 GMT</pubDate>
    <dc:creator>ITWhisperer</dc:creator>
    <dc:date>2024-03-16T13:45:16Z</dc:date>
    <item>
      <title>JSON array and stats command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/JSON-array-and-stats-command/m-p/680906#M232705</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am having trouble generating a stats report based on JSON data containing an array.&amp;nbsp; I want to produce the following report:&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;ErrorCode&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;ErrorMessage&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;Count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;212&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;The image quality is poor&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;680&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;&lt;SPAN&gt;The image could not be found&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN&gt;809&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;&lt;SPAN&gt;Document not detected&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;When I do the stats command, I do not get any results:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;| spath input=jsondata |stats count by "embedded.metadata.data.results{}.notifications.*"&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I have to know the error code value in the array in order to get any stats output.&amp;nbsp; For example:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;| spath input=jsondata |stats count by "embedded.metadata.data.results{}.notifications.809"&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="50%" height="24px"&gt;embedded.metadata.data.results{}.notifications.809&lt;/TD&gt;&lt;TD width="50%" height="24px"&gt;count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%" height="24px"&gt;Document not detected&lt;/TD&gt;&lt;TD width="50%" height="24px"&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here example of the json data&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="100%"&gt;&lt;BR /&gt;{&lt;BR /&gt;"_embedded": {&lt;BR /&gt;&amp;nbsp; "metadata": {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;"environment": {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;"id": "6b3dc"&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; },&lt;BR /&gt;"data": {&lt;BR /&gt;&amp;nbsp; &amp;nbsp;"results": [&lt;BR /&gt;&amp;nbsp; &amp;nbsp; {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;"documentId": "f18a20f1",&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;"notifications": {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;"212": "The image quality was poor"&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;BR /&gt;&amp;nbsp; &amp;nbsp;},&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; &amp;nbsp;"documentId": "f0fdf5e8c",&lt;BR /&gt;&amp;nbsp; &amp;nbsp;"notifications": {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; "680": "The image could not be found"&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;BR /&gt;},&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; &amp;nbsp;"documentId": "95619532",&lt;BR /&gt;&amp;nbsp; &amp;nbsp;"notifications": {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; "809": "Document not detected"&lt;BR /&gt;&amp;nbsp; &amp;nbsp; }&lt;BR /&gt;}&lt;BR /&gt;]&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Thanks in advance for any assistance!!&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 16 Mar 2024 11:25:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/JSON-array-and-stats-command/m-p/680906#M232705</guid>
      <dc:creator>TSplunk</dc:creator>
      <dc:date>2024-03-16T11:25:02Z</dc:date>
    </item>
    <item>
      <title>Re: JSON array and stats command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/JSON-array-and-stats-command/m-p/680910#M232706</link>
      <description>&lt;LI-CODE lang="markup"&gt;| spath _embedded.metadata.data.results{}.notifications output=results
| mvexpand results
| rex field=results "\"(?&amp;lt;ErrorCode&amp;gt;\d+)\":\s*\"(?&amp;lt;ErrorMessage&amp;gt;[^\"]*)\""
| stats count by ErrorCode ErrorMessage&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 16 Mar 2024 13:45:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/JSON-array-and-stats-command/m-p/680910#M232706</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2024-03-16T13:45:16Z</dc:date>
    </item>
    <item>
      <title>Re: JSON array and stats command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/JSON-array-and-stats-command/m-p/680930#M232716</link>
      <description>&lt;P&gt;This is an interesting challenge because of the leading underscore (_) in the root node key. &amp;nbsp;spath can't seem to recognize the path as is. (Could be a subtle bug.) &amp;nbsp;One potential way to work around this is to use text replacement to get rid of the underscore. But I prefer a more syntactic method. &amp;nbsp;In Splunk 9, you can do &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Fromjson" target="_blank" rel="noopener"&gt;fronjson&lt;/A&gt; with an arbitrary prefix so spath can do its job. (You can also use fromjson repeatedly. &amp;nbsp;But with deep paths like this problem, that's undesirable.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| fromjson jsondata prefix=my
| spath input=my_embedded path=metadata.data.results{}
| mvexpand metadata.data.results{}
| spath input=metadata.data.results{}
| foreach notifications.*
    [eval ErrorCode = if(isnull(ErrorCode), "&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;", ErrorCode), ErrorMessage = mvappend(ErrorMessage, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;')]
| stats count by ErrorCode ErrorMessage&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your sample data results in&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ErrorCode&lt;/TD&gt;&lt;TD&gt;ErrorMessage&lt;/TD&gt;&lt;TD&gt;count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;212&lt;/TD&gt;&lt;TD&gt;Document not detected&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;212&lt;/TD&gt;&lt;TD&gt;The image could not be found&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;212&lt;/TD&gt;&lt;TD&gt;The image quality was poor&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;This 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 jsondata = "{
\"_embedded\": {
  \"metadata\": {
     \"environment\": {
         \"id\": \"6b3dc\"
      },
\"data\": {
   \"results\": [
    {
     \"documentId\": \"f18a20f1\",
     \"notifications\": {
         \"212\": \"The image quality was poor\"
       }
   },
{
   \"documentId\": \"f0fdf5e8c\",
   \"notifications\": {
      \"680\": \"The image could not be found\"
     }
},
{
   \"documentId\": \"95619532\",
   \"notifications\": {
      \"809\": \"Document not detected\"
    }
}
]
}
}
}
}"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note: Once you get past that leading underscore in JSON path, you can then use the text manipulation method proposed by&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp;(with a small path correction), like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| fromjson jsondata prefix=my
| spath input=my_embedded path=metadata.data.results{}
| mvexpand metadata.data.results{} ``` not metadata.data.results{}.notifications ```
| rex field=metadata.data.results{} "\"(?&amp;lt;ErrorCode&amp;gt;\d+)\":\s*\"(?&amp;lt;ErrorMessage&amp;gt;[^\"]*)\""
| stats count by ErrorCode ErrorMessage&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But text manipulation on structured data is not as robust because a developer/software can always change format in the future without altering semantics.&lt;/P&gt;</description>
      <pubDate>Sun, 17 Mar 2024 01:07:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/JSON-array-and-stats-command/m-p/680930#M232716</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2024-03-17T01:07:04Z</dc:date>
    </item>
  </channel>
</rss>

