<?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 do bucketing on json data? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/How-to-do-bucketing-on-json-data/m-p/219679#M43121</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;Assuming your field is named json and also assuming the only buckets there are the ones you mentioned (bucket1 = 1,2 and bucket2 = 3,4), give the following a go and see if that helps:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search
| spath input=json path=name
| spath input=json path=date
| spath input=json path=distribution
| rex field=distribution max_match=0 "\"(?&amp;lt;bucket1&amp;gt;\d+)\":[12]"
| rex field=distribution max_match=0 "\"(?&amp;lt;bucket2&amp;gt;\d+)\":[34]"
| stats sum(*) as * by date, name
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults | fields - _time
| eval json = "
{
\"name\":\"srini\",
\"date\":\"20160801\",
\"distribution\": { \"20\":1, \"10\":2, \"10\":1, \"15\":2, 
                            \"10\":3, \"15\":4, \"20\":3, \"30\":4 }
};
{
\"name\":\"srini2\",
\"date\":\"20160802\",
\"distribution\": { \"3\":1, \"1\":2, \"4\":1, \"1\":2, \"1\":1,
                            \"3\":3, \"1\":4, \"4\":3, \"1\":4 }
}
"
| eval json = split(json, ";") 
| mvexpand json
| spath input=json path=name
| spath input=json path=date
| spath input=json path=distribution
| rex field=distribution max_match=0 "\"(?&amp;lt;bucket1&amp;gt;\d+)\":[12]"
| rex field=distribution max_match=0 "\"(?&amp;lt;bucket2&amp;gt;\d+)\":[34]"
| stats sum(*) as * by date, name
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Output:&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/1700i15D10B311178569B/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 10 Aug 2016 14:46:28 GMT</pubDate>
    <dc:creator>javiergn</dc:creator>
    <dc:date>2016-08-10T14:46:28Z</dc:date>
    <item>
      <title>How to do bucketing on json data?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-do-bucketing-on-json-data/m-p/219678#M43120</link>
      <description>&lt;P&gt;I have a json data similar to the example given below&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{
"name":"srini",
"date":"20160801",
"distribution": { "20":1, "10":2, "10":1, "15":2, 
                            "10":3, "15":4, "20":3, "30":4 }
}
{
"name":"srini2",
"date":"20160802",
"distribution": { "3":1, "1":2, "4":1, "1":2, "1":1
                            "3":3, "1":4, "4":3, "1":4 }
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I want to do bucketing based on the keys in the distribution field. Buckets should be like (1,2) and (3,4) and sum up all the values in each bucket. Expected result is like following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;date        bucket1    bucket2
20160801      55         75
20160802      10         10 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Aug 2016 15:02:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-do-bucketing-on-json-data/m-p/219678#M43120</guid>
      <dc:creator>ksrujana</dc:creator>
      <dc:date>2016-08-09T15:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to do bucketing on json data?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-do-bucketing-on-json-data/m-p/219679#M43121</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;Assuming your field is named json and also assuming the only buckets there are the ones you mentioned (bucket1 = 1,2 and bucket2 = 3,4), give the following a go and see if that helps:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search
| spath input=json path=name
| spath input=json path=date
| spath input=json path=distribution
| rex field=distribution max_match=0 "\"(?&amp;lt;bucket1&amp;gt;\d+)\":[12]"
| rex field=distribution max_match=0 "\"(?&amp;lt;bucket2&amp;gt;\d+)\":[34]"
| stats sum(*) as * by date, name
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults | fields - _time
| eval json = "
{
\"name\":\"srini\",
\"date\":\"20160801\",
\"distribution\": { \"20\":1, \"10\":2, \"10\":1, \"15\":2, 
                            \"10\":3, \"15\":4, \"20\":3, \"30\":4 }
};
{
\"name\":\"srini2\",
\"date\":\"20160802\",
\"distribution\": { \"3\":1, \"1\":2, \"4\":1, \"1\":2, \"1\":1,
                            \"3\":3, \"1\":4, \"4\":3, \"1\":4 }
}
"
| eval json = split(json, ";") 
| mvexpand json
| spath input=json path=name
| spath input=json path=date
| spath input=json path=distribution
| rex field=distribution max_match=0 "\"(?&amp;lt;bucket1&amp;gt;\d+)\":[12]"
| rex field=distribution max_match=0 "\"(?&amp;lt;bucket2&amp;gt;\d+)\":[34]"
| stats sum(*) as * by date, name
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Output:&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/1700i15D10B311178569B/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2016 14:46:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-do-bucketing-on-json-data/m-p/219679#M43121</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-08-10T14:46:28Z</dc:date>
    </item>
  </channel>
</rss>

