PostgreSQL 18 reference · JSON Functions

PostgreSQL JSON_TABLE() Function

Turn JSON into a relational table in the FROM clause, with typed columns taken from SQL/JSON paths. Added in PostgreSQL 17.

PostgreSQL 18 Since 17

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

JSON_TABLE(json, path COLUMNS (name type PATH path, ...))

Parameters

ParameterDescription
json The JSON document.
path The row path, such as '$.items[*]'.
COLUMNS Each column's name, type and path.

Return value

A table.

Examples

JSON to rows
SELECT *
FROM JSON_TABLE('{"items":[{"name":"tea","qty":2},{"name":"cake","qty":1}]}',
  '$.items[*]' COLUMNS (name text PATH '$.name', qty int PATH '$.qty')) AS t;

Output

 name | qty
------+-----
 tea  |   2
 cake |   1
(2 rows)

Run this on your computer — see “Get Started” for setup.

Nested arrays
SELECT *
FROM JSON_TABLE('[{"id":1,"tags":["a","b"]}]', '$[*]'
  COLUMNS (id int PATH '$.id', NESTED PATH '$.tags[*]' COLUMNS (tag text PATH '$'))) AS t;

Output

 id | tag
----+-----
  1 | a
  1 | b
(2 rows)

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
PostgreSQL 17Added

Was this page helpful?