Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API
Aggregates the input values from a group into a JSON array, using an optional, user-supplied order.
Syntax
json_arrayagg([DISTINCT] <value> [ORDER BY <sort_expression> [ASC | DESC] [NULLS { FIRST | LAST }] [, ...]] [NULL ON NULL | ABSENT ON NULL])
Arguments
Required
<value>: The single expression collected into the JSON array.
Optional
DISTINCT: Keeps only distinct values. When you useDISTINCT, theORDER BYexpression (including its collation) must match<value>.ORDER BY <sort_expression> [, ...]: Orders the elements within the array (an aggregate-local ordering). Each sort key acceptsASC/DESCandNULLS FIRST/NULLS LAST.ABSENT ON NULL: SkipsNULLinputs so they don't appear in the array. This is the default.NULL ON NULL: KeepsNULLinputs as JSONnullelements.
Returns
A json value containing the aggregated array.
Null handling:
- With the default
ABSENT ON NULL,NULLinputs are dropped. A group whose values are allNULLreturns an empty array ([]). - With
NULL ON NULL,NULLinputs are kept as JSONnullelements.
Considerations
- The
FILTERclause isn't supported forjson_arrayagg. - If no
ORDER BYis specified, the resulting array has an implementation-defined, non-deterministic order. json_arrayaggwithORDER BYisn't supported in combination withROLLUP,CUBE, orGROUPING SETS.- The
<value>is serialized to JSON using the json_scalar conversion rules.
Examples
Aggregate values in a specified order, dropping NULLs
Order elements by id and drop the NULL name (the default behavior).
SELECT json_arrayagg(name ORDER BY id ABSENT ON NULL)
FROM (VALUES (2, 'Grace'), (1, 'Ada'), (3, NULL)) AS people(id, name);
Returns ["Ada","Grace"].
Keep NULLs as JSON null
Use NULL ON NULL to retain NULL inputs as JSON null elements.
SELECT json_arrayagg(v ORDER BY k NULL ON NULL)
FROM (VALUES (20, 1), (NULL, 2), (30, 3)) AS t(v, k);
Returns [20,null,30].
One JSON array per group
Build a JSON array of product names for each category.
SELECT category,
json_arrayagg(product_name ORDER BY product_name)
FROM products
GROUP BY category;