← Back to PostgreSQL, page 2

postgresql-0035 · Medium mid

Turn a JSON array into a relational result with JSON_TABLE in PostgreSQL 17.

JSON_TABLE (new in PostgreSQL 17) maps parts of a JSON document to columns and rows using path expressions, with typed columns, defaults and nested paths. It replaces chains of jsonb_array_elements and ->> casts with one declarative clause, and it is part of the SQL standard.

Full answer

Sample schema and data:

sql
CREATE TABLE api_responses (id int, body jsonb);
INSERT INTO api_responses VALUES (1, '{"orders": [
  {"id": 501, "total": "1250.50", "placed": "2026-11-30", "tags": ["gift"]},
  {"id": 502, "total": "80", "placed": "2026-12-01"},
  {"id": 503, "placed": "2026-12-02", "tags": ["bulk", "b2b"]}]}');
sql
SELECT jt.* FROM api_responses r, JSON_TABLE(r.body, '$.orders[*]' COLUMNS (row_no FOR ORDINALITY, order_id int PATH '$.id', total numeric(10,2) PATH '$.total' DEFAULT 0 ON EMPTY, placed date PATH '$.placed', tag_count int PATH '$.tags.size()' DEFAULT 0 ON EMPTY)) AS jt ORDER BY row_no;

Result on PostgreSQL 17:

text
 row_no | order_id |  total  |   placed   | tag_count
--------+----------+---------+------------+-----------
 1      | 501      | 1250.50 | 2026-11-30 | 1
 2      | 502      | 80.00   | 2026-12-01 | 0
 3      | 503      | 0.00    | 2026-12-02 | 2
(3 rows)
sql
SELECT jt.order_id, jt.tag FROM api_responses r, JSON_TABLE(r.body, '$.orders[*]' COLUMNS (order_id int PATH '$.id', NESTED PATH '$.tags[*]' COLUMNS (tag text PATH '$'))) AS jt ORDER BY order_id, tag NULLS FIRST;

Result:

text
 order_id | tag
----------+------
 501      | gift
 502      | NULL
 503      | b2b
 503      | bulk
(4 rows)

Each order becomes a row with typed columns: the text total '1250.50' is converted to numeric, order 503's missing total falls back to the default 0, and FOR ORDINALITY numbers the rows. The tag count uses a path method; for order 502, which has no tags, the default applies. NESTED PATH expands the tags array: orders with tags produce one row per tag, and order 502 still appears once with a NULL tag. On PostgreSQL 16 and earlier the same result needs jsonb_array_elements plus casts.

sql
CREATE TABLE api_responses (id int, body jsonb);
INSERT INTO api_responses VALUES (1, '{"orders": [
  {"id": 501, "total": "1250.50", "placed": "2026-11-30", "tags": ["gift"]},
  {"id": 502, "total": "80", "placed": "2026-12-01"},
  {"id": 503, "placed": "2026-12-02", "tags": ["bulk", "b2b"]}]}');

SELECT jt.* FROM api_responses r, JSON_TABLE(r.body, '$.orders[*]' COLUMNS (row_no FOR ORDINALITY, order_id int PATH '$.id', total numeric(10,2) PATH '$.total' DEFAULT 0 ON EMPTY, placed date PATH '$.placed', tag_count int PATH '$.tags.size()' DEFAULT 0 ON EMPTY)) AS jt ORDER BY row_no;

SELECT jt.order_id, jt.tag FROM api_responses r, JSON_TABLE(r.body, '$.orders[*]' COLUMNS (order_id int PATH '$.id', NESTED PATH '$.tags[*]' COLUMNS (tag text PATH '$'))) AS jt ORDER BY order_id, tag NULLS FIRST;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • JSON_TABLE is new in PostgreSQL 17
  • Columns are typed with paths
  • DEFAULT ... ON EMPTY handles missing keys
  • NESTED PATH expands inner arrays
  • FOR ORDINALITY numbers rows

Common mistakes

  • Hand-written element loops for simple mapping
  • Ignoring conversion errors on bad values

Follow-ups

  • What do ON ERROR clauses do?
  • How would you write this on PostgreSQL 15?

Tips

  • Mention the version