Skip to main content

groupAsObject (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 it aggregate the JSON elements returns a JSON array.

Syntax

<OBJECT> json:groupAsObject(<STRING|OBJECT> json)
<OBJECT> json:groupAsObject(<STRING|OBJECT> json, <BOOL> distinct)
<OBJECT> json:groupAsObject(<STRING|OBJECT> json, <STRING> enclosing.element)
<OBJECT> json:groupAsObject(<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:groupAsObject("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:groupAsObject("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:groupAsObject("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:groupAsObject("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.