Skip to content

json_object_agg_unique ​

Collect rows into a native JSON object.

Syntax

text
json_object_agg_unique(key,value)

Arguments

Scalar non-NULL keys and native values. Retain SQL NULL values as JSON null. All keys, including skipped NULL-value keys, must be unique; duplicates raise DUPLICATE_JSON_KEY. Plain JSON preserves object input order and nested duplicates. Tuple values use named/fN fields. FILTER, DISTINCT, aggregate ordering, and ordinary windows apply.

Returned value

Native JSON. No input rows returns SQL NULL; rows containing only skipped SQL NULL values produce an empty container.

Examples

sql
CREATE TABLE attributes (name TEXT, value INTEGER);
INSERT INTO attributes VALUES ('a',1),('b',NULL);
sql
SELECT json_object_agg_unique(name,value) FROM attributes FORMAT csv;
text
"{""a"":1,""b"":null}"