<?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 group events into interactions for analysis for JSON events indexed into Splunk? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/How-to-group-events-into-interactions-for-analysis-for-JSON/m-p/419712#M73933</link>
    <description>&lt;P&gt;&lt;STRONG&gt;[UPDATED ANSWER]&lt;/STRONG&gt;&lt;BR /&gt;
Please try the following which will perform an average of quotes as well and give you remaining details (assuming multiple quotes and only one start and end type per correlationId) :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval data="{\"correlationId\": 1, \"type\": \"start\", \"qty\": 10, \"product\": \"product-1\", \"client\": \"client-1\" }; { \"correlationId\": 1, \"type\": \"quote\", \"price\": 100 }; { \"correlationId\": 1, \"type\": \"quote\", \"price\": 101 }; { \"correlationId\": 1, \"type\": \"end\", \"buy\": \"true\", \"qty\": 1, \"price\": 101 };{\"correlationId\": 2, \"type\": \"start\", \"qty\": 5, \"product\": \"product-2\", \"client\": \"client-2\" }; { \"correlationId\": 2, \"type\": \"quote\", \"price\": 120 }; { \"correlationId\": 2, \"type\": \"quote\", \"price\": 80 }; { \"correlationId\": 2, \"type\": \"end\", \"buy\": \"true\", \"qty\": 2, \"price\": 110 }" 
| makemv data delim=";" 
| mvexpand data 
| rename data as _raw 
| spath 
| eval {type}:price"=price , {type}:qty"=qty
| stats avg(quote:price) as AvgQuote values(end:price) as BuyPrice values(start:qty) as start:qty values(end:qty) as end:qty values(client) as client values(product) as product by correlationId
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;P&gt;@Cheetah05, are you interested only in &lt;CODE&gt;start&lt;/CODE&gt; and &lt;CODE&gt;end&lt;/CODE&gt; types? You can try the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;yourBaseSearch&amp;gt; type="start" OR type="end" correlationId=*
| stats latest(price) AS BuyPrice latest(qty) As BuyQty latest(product) as product latest(client) as client latest(buy) as buy BY correlationId 
| table correlationId, product, client, buy, BuyPrice, BuyQty
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Following is run anywhere example based on sample data provided:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval data=" { \"correlationId\": 1, \"type\": \"start\", \"qty\": 10, \"product\": \"product-1\", \"client\": \"client-1\" }; { \"correlationId\": 1, \"type\": \"quote\", \"price\": 100 }; { \"correlationId\": 1, \"type\": \"quote\", \"price\": 101 }; { \"correlationId\": 1, \"type\": \"end\", \"buy\": \"true\", \"qty\": 1, \"price\": 101 }" 
| makemv data delim=";" 
| mvexpand data 
| rename data as _raw 
| spath 
| search type="start" OR type="end" 
| stats latest(price) AS BuyPrice latest(qty) As BuyQty latest(product) as product latest(client) as client latest(buy) as buy BY correlationId 
| table correlationId, product, client, buy, BuyPrice, BuyQty
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 27 May 2018 11:45:41 GMT</pubDate>
    <dc:creator>niketn</dc:creator>
    <dc:date>2018-05-27T11:45:41Z</dc:date>
    <item>
      <title>How to group events into interactions for analysis for JSON events indexed into Splunk?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-group-events-into-interactions-for-analysis-for-JSON/m-p/419711#M73932</link>
      <description>&lt;P&gt;I have a series of differently-shaped JSON events indexed into Splunk (as JSON). They have a &lt;STRONG&gt;correlation id to link the events into "interactions"&lt;/STRONG&gt;.&lt;/P&gt;

&lt;P&gt;Example events that form an interaction:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{ "correlationId": 1, "type": "start", "qty": 10, "product": "product-1", "client": "client-1" }
{ "correlationId": 1, "type": "quote", "price": 100 }
{ "correlationId": 1, "type": "quote", "price": 101 }
{ "correlationId": 1, "type": "end", "buy": "true , "qty": 1, "price": 101 }
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Not every interaction will be complete, there might not be "quote" or "end" types.&lt;/P&gt;

&lt;P&gt;Imagine I'm looking for every "interaction", that has an end which is buy, last price &amp;gt; 10, extracting the fields: start-qty, end-qty, price, product, client&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;The fields "price" and "qty" have different meanings depending on which event type it belongs to&lt;/STRONG&gt;, so I need some way to rename these events before I do my search!&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Is the idea to flatten it into one record by naming each field manually?&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;I could write something like (might be a few syntactic errors):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eventstats latest(price) AS BuyPrice latest(qty) As BuyQty BY correlationId | where type="start" | table correlationId, product, client, buy, BuyPrice, BuyQty
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is flattening everything onto the "start" type and using that record to build my results table. &lt;STRONG&gt;Is this the correct way of handling this?&lt;/STRONG&gt; I'm conscious of the fact this will be probably doing multiple iterations of the data, when infact you could probably do just one iteration to build the result set. &lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;How do I handle the fact that technically the "BuyPrice" column could have a way from a "quote" which was never bought?&lt;/STRONG&gt; or what if I wanted to introduce the start type "qty" aswell?&lt;/P&gt;

&lt;P&gt;Also - &lt;STRONG&gt;is it possible, once I've built my query, to give it an alias that I can use as my base search result set so I don't have to keep writing the query?&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 27 May 2018 10:29:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-group-events-into-interactions-for-analysis-for-JSON/m-p/419711#M73932</guid>
      <dc:creator>Cheetah05</dc:creator>
      <dc:date>2018-05-27T10:29:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to group events into interactions for analysis for JSON events indexed into Splunk?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-group-events-into-interactions-for-analysis-for-JSON/m-p/419712#M73933</link>
      <description>&lt;P&gt;&lt;STRONG&gt;[UPDATED ANSWER]&lt;/STRONG&gt;&lt;BR /&gt;
Please try the following which will perform an average of quotes as well and give you remaining details (assuming multiple quotes and only one start and end type per correlationId) :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval data="{\"correlationId\": 1, \"type\": \"start\", \"qty\": 10, \"product\": \"product-1\", \"client\": \"client-1\" }; { \"correlationId\": 1, \"type\": \"quote\", \"price\": 100 }; { \"correlationId\": 1, \"type\": \"quote\", \"price\": 101 }; { \"correlationId\": 1, \"type\": \"end\", \"buy\": \"true\", \"qty\": 1, \"price\": 101 };{\"correlationId\": 2, \"type\": \"start\", \"qty\": 5, \"product\": \"product-2\", \"client\": \"client-2\" }; { \"correlationId\": 2, \"type\": \"quote\", \"price\": 120 }; { \"correlationId\": 2, \"type\": \"quote\", \"price\": 80 }; { \"correlationId\": 2, \"type\": \"end\", \"buy\": \"true\", \"qty\": 2, \"price\": 110 }" 
| makemv data delim=";" 
| mvexpand data 
| rename data as _raw 
| spath 
| eval {type}:price"=price , {type}:qty"=qty
| stats avg(quote:price) as AvgQuote values(end:price) as BuyPrice values(start:qty) as start:qty values(end:qty) as end:qty values(client) as client values(product) as product by correlationId
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;P&gt;@Cheetah05, are you interested only in &lt;CODE&gt;start&lt;/CODE&gt; and &lt;CODE&gt;end&lt;/CODE&gt; types? You can try the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;yourBaseSearch&amp;gt; type="start" OR type="end" correlationId=*
| stats latest(price) AS BuyPrice latest(qty) As BuyQty latest(product) as product latest(client) as client latest(buy) as buy BY correlationId 
| table correlationId, product, client, buy, BuyPrice, BuyQty
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Following is run anywhere example based on sample data provided:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval data=" { \"correlationId\": 1, \"type\": \"start\", \"qty\": 10, \"product\": \"product-1\", \"client\": \"client-1\" }; { \"correlationId\": 1, \"type\": \"quote\", \"price\": 100 }; { \"correlationId\": 1, \"type\": \"quote\", \"price\": 101 }; { \"correlationId\": 1, \"type\": \"end\", \"buy\": \"true\", \"qty\": 1, \"price\": 101 }" 
| makemv data delim=";" 
| mvexpand data 
| rename data as _raw 
| spath 
| search type="start" OR type="end" 
| stats latest(price) AS BuyPrice latest(qty) As BuyQty latest(product) as product latest(client) as client latest(buy) as buy BY correlationId 
| table correlationId, product, client, buy, BuyPrice, BuyQty
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 27 May 2018 11:45:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-group-events-into-interactions-for-analysis-for-JSON/m-p/419712#M73933</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-05-27T11:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to group events into interactions for analysis for JSON events indexed into Splunk?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-group-events-into-interactions-for-analysis-for-JSON/m-p/419713#M73934</link>
      <description>&lt;P&gt;@niketnilay, for some reason I am not allowed to comment so I can on reply in the form of an answer...&lt;/P&gt;

&lt;P&gt;Whilst I get the sentiment, not the answer I was looking for (perhaps my fault for the wording of the question). I may still want say the avg(price) from the quote type or the start type qty.&lt;/P&gt;</description>
      <pubDate>Mon, 28 May 2018 07:35:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-group-events-into-interactions-for-analysis-for-JSON/m-p/419713#M73934</guid>
      <dc:creator>Cheetah05</dc:creator>
      <dc:date>2018-05-28T07:35:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to group events into interactions for analysis for JSON events indexed into Splunk?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-group-events-into-interactions-for-analysis-for-JSON/m-p/419714#M73935</link>
      <description>&lt;P&gt;@Cheetah05, please try the updated answer below. I am surprised, usually answers go for moderation but comment should not be blocked.&lt;/P&gt;</description>
      <pubDate>Mon, 28 May 2018 11:55:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-group-events-into-interactions-for-analysis-for-JSON/m-p/419714#M73935</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-05-28T11:55:31Z</dc:date>
    </item>
  </channel>
</rss>

