<?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 can I count mv fields by type? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481446#M196007</link>
    <description>&lt;P&gt;Actually I didn't add double quotes while asking this question, the data in production server does have it.&lt;BR /&gt;
I was trying it with stats, but wasn't giving expected result. Thank You for you help. &lt;/P&gt;</description>
    <pubDate>Tue, 03 Mar 2020 23:30:37 GMT</pubDate>
    <dc:creator>akshaysaraf</dc:creator>
    <dc:date>2020-03-03T23:30:37Z</dc:date>
    <item>
      <title>How can I count mv fields by type?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481441#M196002</link>
      <description>&lt;P&gt;My data looks like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{
 parent_id: 1
 child_info: [
             {
               id: 123,
               status: "PASS"
             },
             {
               id: 456,
               status: "FAIL"
             }
          ]
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I am trying to print the result in format:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;parent ID  |  Child_Passed_count  |  Child_Failed_count
 1                 1                           1
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Mar 2020 01:11:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481441#M196002</guid>
      <dc:creator>akshaysaraf</dc:creator>
      <dc:date>2020-03-03T01:11:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can I count mv fields by type?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481442#M196003</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval jsonTest = "{
  \"parent_id\": 1,
  \"child_info\": [
              {
                \"id\": \"123\",
                \"status\": \"PASS\"
                      },
              {
                \"id\": \"456\",
                \"status\": \"FAIL\"
                      },
                  {
                \"id\": \"457\",
                \"status\": \"FAIL\"
                      }
      ]
 }" 
| spath input=jsonTest path=parent_id output=parent_id 
| spath input=jsonTest output=id_initial path=child_info{}.id 
| spath input=jsonTest output=status_initial path=child_info{}.status 
| eval val = mvzip(id_initial, status_initial) 
| mvexpand val
| eval mvVal = split(val, ",") 
| eval id = mvindex(mvVal, 0) 
| eval status = mvindex(mvVal, 1) 
| stats count(eval(status=="PASS")) as Child_Passed_count count(eval(status=="FAIL")) as Child_Failed_count by parent_id
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;parent_id   Child_Passed_count  Child_Failed_count&lt;BR /&gt;
1                                  1                               2&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 04:23:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481442#M196003</guid>
      <dc:creator>anmolpatel</dc:creator>
      <dc:date>2020-09-30T04:23:28Z</dc:date>
    </item>
    <item>
      <title>Re: How can I count mv fields by type?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481443#M196004</link>
      <description>&lt;P&gt;Thank you, but in my production server I have about 500,000 events which means 500,000 parent ID's, and each parent has about 200 child records. When I mvexpand for such fields I run out of memory and the search truncates my result. Is there a way of doing it without mvexpand?&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 05:07:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481443#M196004</guid>
      <dc:creator>akshaysaraf</dc:creator>
      <dc:date>2020-03-03T05:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: How can I count mv fields by type?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481444#M196005</link>
      <description>&lt;P&gt;Try using this &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;your base search&amp;gt;
  | rex "(?&amp;lt;json&amp;gt;\{.+)" | spath input=json | fields - json
  | rename child_info{}.id AS child_id child_info{}.status AS child_status
  | mvexpand child_status
  | stats count(eval(child_status=="PASS")) as Child_Passed_count count(eval(child_status=="FAIL")) as Child_Failed_count by parent_id
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Mar 2020 05:51:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481444#M196005</guid>
      <dc:creator>sumanssah</dc:creator>
      <dc:date>2020-03-03T05:51:30Z</dc:date>
    </item>
    <item>
      <title>Re: How can I count mv fields by type?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481445#M196006</link>
      <description>&lt;P&gt;This is another case of FIX YOUR BROKEN JSON.  Here is how you can manually fix it but really, get it fixed at the source:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval _raw = "{
   parent_id: 1
   child_info: [
               {
                 id: 123,
                 status: \"PASS\"
                           },
               {
                 id: 456,
                 status: \"FAIL\"
                           },
              {
                 id: 457,
                 status: \"FAIL\"
                           }
       ]
  }" 

| rename COMMENT AS "Everything above generates BROKEN JSON; everything below is your solution"

| rex mode=sed "s/([\r\n\s]+)([^\r\n\s\"]+):/\1\"\2\":/g s/([^{\[\r\n\s:,])([\r\n\s]+\")/\1,\2/g"
| kv
| chart count BY parent_id child_info{}.status
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Mar 2020 06:09:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481445#M196006</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2020-03-03T06:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: How can I count mv fields by type?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481446#M196007</link>
      <description>&lt;P&gt;Actually I didn't add double quotes while asking this question, the data in production server does have it.&lt;BR /&gt;
I was trying it with stats, but wasn't giving expected result. Thank You for you help. &lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 23:30:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481446#M196007</guid>
      <dc:creator>akshaysaraf</dc:creator>
      <dc:date>2020-03-03T23:30:37Z</dc:date>
    </item>
    <item>
      <title>Re: How can I count mv fields by type?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481447#M196008</link>
      <description>&lt;P&gt;That would have saved me 90% of my effort.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 04:23:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-count-mv-fields-by-type/m-p/481447#M196008</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2020-03-04T04:23:57Z</dc:date>
    </item>
  </channel>
</rss>

