SQL/JSON JSON_OBJECTAGG And JSON_ARRAYAGG
Aggregate rows into JSONB objects and ordered arrays using PostgreSQL 16 JSON_OBJECTAGG and JSON_ARRAYAGG with NULL handling and WITH UNIQUE KEYS.
Code
CREATE TEMP TABLE service_flags (
service text NOT NULL,
flag_name text NOT NULL,
flag_val text,
priority integer NOT NULL
);
INSERT INTO service_flags VALUES
('auth', 'mfa_mode', 'totp', 2),
('auth', 'audit_note', NULL, 1),
('edge', 'rate_burst', '40', 1),
('edge', 'canary_tier', NULL, 2);
-- Defaults differ in SQL/JSON: JSON_OBJECTAGG keeps NULLs; JSON_ARRAYAGG omits NULLs
SELECT
service,
JSON_OBJECTAGG(flag_name VALUE flag_val RETURNING jsonb) AS obj_default_nulls,
JSON_OBJECTAGG(flag_name VALUE flag_val ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb) AS obj_compact,
JSON_ARRAYAGG(flag_val ORDER BY priority RETURNING jsonb) AS arr_default_absent,
JSON_ARRAYAGG(flag_val ORDER BY priority NULL ON NULL RETURNING jsonb) AS arr_keep_nulls
FROM service_flags
GROUP BY service
ORDER BY service;
-- WITH UNIQUE KEYS throws when duplicate keys appear within the same group
INSERT INTO service_flags VALUES ('auth', 'mfa_mode', 'webauthn', 3);
DO $$
DECLARE
res jsonb;
BEGIN
SELECT JSON_OBJECTAGG(flag_name VALUE flag_val WITH UNIQUE KEYS RETURNING jsonb)
INTO res
FROM service_flags
WHERE service = 'auth';
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'duplicate key rejected (%): %', SQLSTATE, SQLERRM;
END $$;Code flow overview — SQL/JSON JSON_OBJECTAGG And JSON_ARRAYAGG
SELECT / INSERT / CREATE statements share the script, touching service_flags and res.
service_flags establishes transient connection-bound temporary tables inline right here without needing external setup and removes them cleanly when the session ends.
Mechanically, the statements are built from: a WHERE clause that keeps only the rows a statement is meant to touch; a GROUP BY that collapses matching rows into one output row per group; an ORDER BY that fixes the output order; a RETURNING clause that hands the affected rows back to the caller; NOT NULL qualifiers on the required columns; UNIQUE constraints that keep duplicate values out.
Applying it
The file sets up its own scratch surface: it builds the session-local tables it works with (service_flags), so one full run against a throwaway database is enough.
Statements that matter
DO $$
DECLARE
res jsonb;
BEGIN
SELECT JSON_OBJECTAGG(flag_name VALUE flag_val WITH UNIQUE KEYS RETURNING jsonb)
INTO res
FROM service_flags
WHERE service = 'auth';
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'duplicate key rejected (%): %', SQLSTATE, SQLERRM;
END $$;
INSERT INTO service_flags VALUES
('auth', 'mfa_mode', 'totp', 2),
('auth', 'audit_note', NULL, 1),
('edge', 'rate_burst', '40', 1),
('edge', 'canary_tier', NULL, 2);
CREATE TEMP TABLE service_flags (
service text NOT NULL,
flag_name text NOT NULL,
flag_val text,
priority integer NOT NULL
);
Interactive Tools for This Topic
FAQ
- What relations does the SQL touch?
- `service_flags`, `res` — referenced directly, no join needed.
Report a problem with this exampleOpens a pre-filled issue on GitHub.