<?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: Get count of how how many of a field has a true value. in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661298#M228311</link>
    <description>&lt;P&gt;You should not use&amp;nbsp;&lt;U&gt;&lt;FONT face="andale mono,times"&gt;foreach *&lt;/FONT&gt;&lt;/U&gt;. &amp;nbsp;tag::event is a meta field and foreach will not handle those. &amp;nbsp;It is quite obvious that your data also contain other irrelevant fields. &amp;nbsp;If you know those tag names, enumerate it. (Read the document.)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| foreach flag1 flag2 flag3 ... flagX
    [eval trueflag = mvappend(trueflag, if(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; == "true", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]
| stats count by trueflag&lt;/LI-CODE&gt;&lt;P&gt;Alternatively, you probably do not care about other fields. &amp;nbsp;Remove them so foreach will not be bombed.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| fields loggingObject.responseJson
| spath input=loggingObject.responseJson
| foreach *
    [eval trueflag = mvappend(trueflag, if(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; == "true", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]
| stats count by trueflag&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 19 Oct 2023 06:18:49 GMT</pubDate>
    <dc:creator>yuanliu</dc:creator>
    <dc:date>2023-10-19T06:18:49Z</dc:date>
    <item>
      <title>Get count of how how many of a field has a true value.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661251#M228291</link>
      <description>&lt;P&gt;I have a response that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;{"meta":{"code":400},"flag1":false,"flag2":false,"flag3":true}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are more than 3 flags, but this is an example.&lt;/P&gt;&lt;P&gt;Assuming that there is only one that is true in each response, I want to get a count of which flag is true the most times, in descending order.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Oct 2023 17:20:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661251#M228291</guid>
      <dc:creator>alakhotia</dc:creator>
      <dc:date>2023-10-18T17:20:02Z</dc:date>
    </item>
    <item>
      <title>Re: Get count of how how many of a field has a true value.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661257#M228296</link>
      <description>&lt;P&gt;Something like&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| foreach flag*
    [eval trueflag = mvappend(trueflag, if(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; == "true", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]
| stats count by trueflag&lt;/LI-CODE&gt;&lt;P&gt;The wildcard expression will depend on actual field names. (Worst comes you iterate over non-flag fields; alternatively, you enumerate all possible flags.) See &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Foreach" target="_blank" rel="noopener"&gt;foreach&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Oct 2023 18:41:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661257#M228296</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-10-18T18:41:06Z</dc:date>
    </item>
    <item>
      <title>Re: Get count of how how many of a field has a true value.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661261#M228300</link>
      <description>&lt;P&gt;Thanks.&amp;nbsp; My response object is extracted to responseJson.&lt;/P&gt;&lt;P&gt;How do I iterate over any possible field name in responseJson?&amp;nbsp; What am I doing wrong below?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;| eval responseJson='loggingObject.responseJson'&lt;BR /&gt;| foreach *&lt;BR /&gt;[eval trueflag = mvappend(trueflag, if(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; == "true", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]&lt;BR /&gt;| stats count by trueflag&lt;/P&gt;</description>
      <pubDate>Wed, 18 Oct 2023 18:55:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661261#M228300</guid>
      <dc:creator>alakhotia</dc:creator>
      <dc:date>2023-10-18T18:55:17Z</dc:date>
    </item>
    <item>
      <title>Re: Get count of how how many of a field has a true value.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661271#M228305</link>
      <description>&lt;P&gt;Do you mean to say that Splunk gives you a field named&amp;nbsp;&lt;SPAN&gt;'loggingObject.responseJson' with that JSON object as value? &amp;nbsp;In that case, you need to first extract from JSON with &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Spath" target="_blank" rel="noopener"&gt;spath&lt;/A&gt;. (A newer alternative is &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Fromjson" target="_blank" rel="noopener"&gt;fromjson&lt;/A&gt;.)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| spath input=loggingObject.responseJson
| foreach flag*
    [eval trueflag = mvappend(trueflag, if(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; == "true", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]
| stats count by trueflag&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here 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
| fields - _time
| eval loggingObject.responseJson = "{\"meta\":{\"code\":400},\"flag1\":false,\"flag2\":false,\"flag3\":true}"
``` data emulation above ```&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Oct 2023 20:56:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661271#M228305</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-10-18T20:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: Get count of how how many of a field has a true value.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661274#M228307</link>
      <description>&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I hardcode data like you've done, and I add escape backslash quotes, it works.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| fields - _time

| eval loggingObject.responseJson = "{\"meta\":{\"code\":400},\"flag1\":false,\"flag2\":false,\"flag3\":true,\"flag3status\":\"3\",\"flag4\":false,\"flag5\":false,\"flag6\":false,\"flag7\":false, \"flag7reason\":\"xyz\"}"
| spath input=loggingObject.responseJson
| foreach *
[eval trueflag = mvappend(trueflag, if(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; == "true", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]
| stats count by trueflag&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I use my real data results, I do get results, but also some splunk errors:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval responseJson='loggingObject.responseJson'
| spath input=responseJson
| foreach *
    [eval trueflag = mvappend(trueflag, if(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; == "true", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]
| stats count by trueflag&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Errors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[shsplnkprnap008,shsplnkprnap009,shsplnkprnap010,shsplnkprnap011,shsplnkprnap012,shsplnkprnap013] Failed to parse templatized search for field 'tag::eventtype'
[shsplnkprnap008,shsplnkprnap009,shsplnkprnap011,shsplnkprnap012,shsplnkprnap013] Failed to parse templatized search for field 'loggingObject.methodParams{}.className'
[shsplnkprnap008,shsplnkprnap009,shsplnkprnap011,shsplnkprnap012,shsplnkprnap013] Failed to parse templatized search for field 'loggingObject.methodParams{}.value'
[shsplnkprnap008,shsplnkprnap009,shsplnkprnap012,shsplnkprnap013] Failed to parse templatized search for field 'loggingObject.requestHeaders.user-agent'
[shsplnkprnap008,shsplnkprnap009,shsplnkprnap012,shsplnkprnap013] Failed to parse templatized search for field 'loggingObject.requestHeaders.x-forwarded-for'
[shsplnkprnap008,shsplnkprnap009,shsplnkprnap013] Failed to parse templatized search for field 'Device-ID'
[shsplnkprnap008,shsplnkprnap009,shsplnkprnap013] Failed to parse templatized search for field 'valid-beacon-dept-count'
[shsplnkprnap009] Failed to parse templatized search for field 'steps{}'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am able to do something like this without splunk errors;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval responseJson='loggingObject.responseJson'

| stats count by responseJson&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 18 Oct 2023 22:24:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661274#M228307</guid>
      <dc:creator>alakhotia</dc:creator>
      <dc:date>2023-10-18T22:24:53Z</dc:date>
    </item>
    <item>
      <title>Re: Get count of how how many of a field has a true value.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661298#M228311</link>
      <description>&lt;P&gt;You should not use&amp;nbsp;&lt;U&gt;&lt;FONT face="andale mono,times"&gt;foreach *&lt;/FONT&gt;&lt;/U&gt;. &amp;nbsp;tag::event is a meta field and foreach will not handle those. &amp;nbsp;It is quite obvious that your data also contain other irrelevant fields. &amp;nbsp;If you know those tag names, enumerate it. (Read the document.)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| foreach flag1 flag2 flag3 ... flagX
    [eval trueflag = mvappend(trueflag, if(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; == "true", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]
| stats count by trueflag&lt;/LI-CODE&gt;&lt;P&gt;Alternatively, you probably do not care about other fields. &amp;nbsp;Remove them so foreach will not be bombed.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| fields loggingObject.responseJson
| spath input=loggingObject.responseJson
| foreach *
    [eval trueflag = mvappend(trueflag, if(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; == "true", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;", null()))]
| stats count by trueflag&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 19 Oct 2023 06:18:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Get-count-of-how-how-many-of-a-field-has-a-true-value/m-p/661298#M228311</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-10-19T06:18:49Z</dc:date>
    </item>
  </channel>
</rss>

