JSON_OBJECTAGG
JSON aggregation
What changes
- Input
- key:value pairs over each group's rows
- Output or change
- one JSON object per group
- Return type
- Returns a valid JSON object string of nvarchar(max) type. If the RETURNING JSON option is included then the JSON object is returned as json type.
Syntax at a glance
JSON_OBJECTAGG ( json_key_value [ json_null_clause ] [ RETURNING JSON ] )
Work example
Build one JSON object from grouped key and value rows.
Make keys unique within each group and define how null values should appear.
Check your work JSON_OBJECTAGG is available in SQL Server 2025 and duplicate keys need a deliberate input rule.
Open Microsoft Learn for full syntax, permissions, and details for your version.