Skip to main content

group (Aggregate Function)

This function aggregates the JSON elements and returns a JSON object by adding enclosing.element if it is provided. If enclosing.element is not provided, then it aggregate the JSON elements returns a JSON array.

Syntax

<OBJECT> json:group(<STRING|OBJECT> json)
<OBJECT> json:group(<STRING|OBJECT> json, <BOOL> distinct)
<OBJECT> json:group(<STRING|OBJECT> json, <STRING> enclosing.element)
<OBJECT> json:group(<STRING|OBJECT> json, <STRING> enclosing.element, <BOOL> distinct)

Query Parameters

NameDescriptionDefault ValuePossible Data TypesOptionalDynamic
jsonThe JSON element that needs to be aggregated.STRING OBJECTNoYes
enclosing.elementThe JSON element used to enclose the aggregated JSON elements.EMPTY_STRINGSTRINGYesYes
distinctThis is used to only have distinct JSON elements in the concatenated JSON object/array that is returned.falseBOOLYesYes

Example 1

select json:group("json") as groupedJSONArray
from InputStream WINDOW SLIDING_LENGTH(5)
input OutputStream;

When we input events having values for the json as {"date":"2013-11-19","time":"10:30"} and {"date":"2013-11-19","time":"12:20"}, it returns [{"date":"2013-11-19","time":"10:30"}{"date":"2013-11-19","time":"12:20"}] to the OutputStream.

Example 2

select json:group("json", true) as groupedJSONArray
from InputStream WINDOW SLIDING_LENGTH(5)
input OutputStream;

When we input events having values for the json as {"date":"2013-11-19","time":"10:30"} and {"date":"2013-11-19","time":"10:30"}, it returns [{"date":"2013-11-19","time":"10:30"}] to the OutputStream.

Example 3

select json:group("json", "result") as groupedJSONArray
from InputStream WINDOW SLIDING_LENGTH(5)
input OutputStream;

When we input events having values for the json as {"date":"2013-11-19","time":"10:30"} and {"date":"2013-11-19","time":"12:20"}, it returns {"result":[{"date":"2013-11-19","time":"10:30"},{"date":"2013-11-19","time":"12:20"}} to the OutputStream.

Example 4

select json:group("json", "result", true) as groupedJSONArray
from InputStream WINDOW SLIDING_LENGTH(5)
input OutputStream;

When we input events having values for the json as {"date":"2013-11-19","time":"10:30"} and {"date":"2013-11-19","time":"10:30"}, it returns {"result":[{"date":"2013-11-19","time":"10:30"}]} to the OutputStream.