Groups an array of objects $elements using an expression $expr as the group key.
The $expr expression is applied to each element in the array $elements and the
resulting value is used as a group key.
The result is an object whose keys are the unique set of string keys and whose respective values are an array of objects array matching the group criteria.
Objects that do not match the group criteria are discarded from the output.
This includes objects for which applying the $expr expression evaluates to null.
If the result of applying the $expr expression against the current array element
results in type other than string or null, a type error MUST be raised.
group_by function
Abstract
This JEP introduces a new
group_by()
function.Specification
group_by
Groups an array of objects
$elements
using an expression$expr
as the group key. The$expr
expression is applied to each element in the array$elements
and the resulting value is used as a group key.The result is an object whose keys are the unique set of string keys and whose respective values are an array of objects array matching the group criteria.
Objects that do not match the group criteria are discarded from the output. This includes objects for which applying the
$expr
expression evaluates tonull
.If the result of applying the
$expr
expression against the current array element results in type other thanstring
ornull
, a type error MUST be raised.Examples
Given the following input JSON document:
Example:
group_by(items, &spec.nodeName)
|{"node_01": [{"spec": {"nodeName": "node_01", "other": "values_01"}}, {"spec": {"nodeName": "node_01", "other": "values_04"}}], "node_02": [{"spec": {"nodeName": "node_02", "other": "values_02"}}], "node_03": [{"spec": {"nodeName": "node_03", "other": "values_03"}}]}
Given the following input JSON document:
Here are a couple of other examples:
group_by(array, &name)
|{"one":[{"name":"one","b":true}],"two":[{"name":"two","b":false}]}
group_by(array, &b)
|invalid-type