jsonb_object_agg Builds Keyed JSON Objects
Aggregate grouped rows into keyed JSON objects with jsonb_object_agg, expand them again, and deduplicate repeated keys first.
Code
-- jsonb_object_agg builds keyed JSON objects from grouped key-value rows
WITH products (category, sku, price) AS (
VALUES
('tools', 'hammer', 19.90),
('tools', 'wrench', 12.50),
('garden', 'rake', 24.00),
('garden', 'hose', 31.75)
)
SELECT
category,
jsonb_object_agg(sku, price ORDER BY sku) AS price_map
FROM products
GROUP BY category
ORDER BY category;
-- 读回对象:jsonb_each_text 展开键值对
WITH products (category, sku, price) AS (
VALUES ('tools', 'hammer', 19.90), ('tools', 'wrench', 12.50)
), grouped AS (
SELECT category, jsonb_object_agg(sku, price) AS price_map
FROM products
GROUP BY category
)
SELECT category, key, value
FROM grouped, jsonb_each_text(price_map)
ORDER BY category, key;
-- 键重复时聚合会报错;先去重再聚合
WITH events (sku, price, recorded_at) AS (
VALUES
('A-1', 10.00, timestamptz '2026-11-01 09:00+00'),
('A-1', 12.00, timestamptz '2026-11-02 09:00+00')
), latest AS (
SELECT DISTINCT ON (sku) sku, price
FROM events
ORDER BY sku, recorded_at DESC
)
SELECT jsonb_object_agg(sku, price) AS latest_prices FROM latest;How it works
Several statements share this file — SELECT, with products, grouped, events and latest among the objects involved.
Breaking the statements into their working parts: a CTE (WITH … AS) that names an intermediate result before the main query; a GROUP BY that collapses matching rows into one output row per group; an ORDER BY that fixes the output order; a DISTINCT qualifier that collapses duplicate rows.
Using it
Run it in the products database context.
Key statements
WITH products (category, sku, price) AS (
VALUES ('tools', 'hammer', 19.90), ('tools', 'wrench', 12.50)
), grouped AS (
SELECT category, jsonb_object_agg(sku, price) AS price_map
FROM products
GROUP BY category
)
SELECT category, key, value
FROM grouped, jsonb_each_text(price_map)
ORDER BY category, key;
WITH events (sku, price, recorded_at) AS (
VALUES
('A-1', 10.00, timestamptz '2026-11-01 09:00+00'),
('A-1', 12.00, timestamptz '2026-11-02 09:00+00')
), latest AS (
SELECT DISTINCT ON (sku) sku, price
FROM events
ORDER BY sku, recorded_at DESC
)
SELECT jsonb_object_agg(sku, price) AS latest_prices FROM latest;
WITH products (category, sku, price) AS (
VALUES
('tools', 'hammer', 19.90),
('tools', 'wrench', 12.50),
('garden', 'rake', 24.00),
('garden', 'hose', 31.75)
)
SELECT
category,
jsonb_object_agg(sku, price ORDER BY sku) AS price_map
FROM products
GROUP BY category
ORDER BY category;
Interactive Tools for This Topic
Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.CSV to JSON ConverterConvert comma-separated values into a JSON array of objects using the first row as field names, with support for quoted and escaped fields.
FAQ
- Which tables does the SQL touch?
- `products`, `grouped`, `events`, `latest`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.