JSON_ARRAYAGG
JSON aggregation
What changes
- Input
- value expression over each group's rows, optional ORDER BY
- Output or change
- one JSON array per group
- Return type
- Returns a valid JSON array string of nvarchar(max) type. If the RETURNING json option is included then the JSON array is returned as json type.
Syntax at a glance
JSON_ARRAYAGG (value_expression [ order_by_clause ] [ json_null_clause ] [ RETURNING json ] )
Work example
Package grouped row values as one JSON array for a document response.
Use an explicit order when array position matters and decide whether null values belong in the output.
Check your work JSON_ARRAYAGG is available in SQL Server 2025 and array order needs an explicit ordering rule.
Open Microsoft Learn for full syntax, permissions, and details for your version.