Sub topic 12
JSON_VALUE, JSON_QUERY, and JSON_TABLE in PostgreSQL 17
This chapter assumes basic SELECT, JOIN, and WHERE usage, PostgreSQL types, and JSON syntax. You should also recognize jsonb access operators and SQL/JSON path expressions such as $.items[*].sku.
PostgreSQL 17 adds standard SQL/JSON functions that make scalar extraction, document extraction, and relational conversion clearer:
| Function | Main result | Typical use |
|---|---|---|
| `JSON_VALUE` | One SQL scalar | Read a number, string, or Boolean |
| `JSON_QUERY` | A JSON value | Read an object or array |
| `JSON_TABLE` | Rows and columns | Turn JSON into a queryable table |
Extracting scalar values with JSON_VALUE
JSON_VALUE evaluates a SQL/JSON path and returns one scalar value. By default, the result is text. Use RETURNING when the result should have a specific SQL type.
SELECT
json_value(
'{"order_id": 17, "total": 42.50, "paid": true}'::jsonb,
'$.order_id' RETURNING bigint
) AS order_id,
json_value(
'{"order_id": 17, "total": 42.50, "paid": true}'::jsonb,
'$.total' RETURNING numeric
) AS total,
json_value(
'{"order_id": 17, "total": 42.50, "paid": true}'::jsonb,
'$.paid' RETURNING boolean
) AS paid;A path that matches an object or array is not a scalar match. You can control missing values and conversion failures with ON EMPTY and ON ERROR.
SELECT json_value(
'{"quantity": "unknown"}'::jsonb,
'$.quantity' RETURNING integer
DEFAULT 0 ON ERROR
) AS quantity;ON EMPTY handles a path that finds nothing. ON ERROR handles problems such as converting "unknown" to integer. The default behavior is usually NULL, but explicit behavior makes an import or reporting query easier to review.
Extracting JSON with JSON_QUERY
Use JSON_QUERY when the result should remain an object or array. Its result is JSON rather than an ordinary SQL scalar.
SELECT json_query(
'{"items":[{"sku":"A1","qty":2},{"sku":"B7","qty":1}]}'::jsonb,
'$.items'
) AS items;A path can produce multiple values. A wrapper tells PostgreSQL how to represent those values. WITH CONDITIONAL ARRAY WRAPPER returns an array when necessary while avoiding an unnecessary extra array around an existing array.
SELECT json_query(
'{"items":[{"sku":"A1","qty":2},{"sku":"B7","qty":1}]}'::jsonb,
'$.items[*].sku' WITH CONDITIONAL ARRAY WRAPPER
) AS skus;Use JSON_VALUE for a value that your SQL expressions should compare or aggregate. Use JSON_QUERY when the JSON structure itself must be returned to an application or passed to another JSON operation.
Turning documents into rows with JSON_TABLE
JSON_TABLE maps a JSON document to columns. Each column has a SQL type and a path. NESTED PATH creates rows for array elements.
SELECT jt.customer, jt.sku, jt.qty
FROM JSON_TABLE(
'{
"customer": "Ada",
"items": [
{"sku": "A1", "qty": 2},
{"sku": "B7", "qty": 1}
]
}'::jsonb,
'$'
COLUMNS (
customer text PATH '$.customer',
NESTED PATH '$.items[*]' COLUMNS (
sku text PATH '$.sku',
qty integer PATH '$.qty'
)
)
) AS jt;The query returns one row for each item. The customer value is repeated for each item row. This is useful when a JSON payload must be joined to relational tables, filtered with ordinary SQL, or aggregated.
JSON_TABLE also supports FOR ORDINALITY when the original array position matters:
SELECT jt.position, jt.sku
FROM JSON_TABLE(
'[{"sku":"A1"},{"sku":"B7"}]'::jsonb,
'$[*]'
COLUMNS (
position integer FOR ORDINALITY,
sku text PATH '$.sku'
)
) AS jt;Choosing between the functions
Choose the function based on the shape of the result, not only on the input type. JSON_VALUE is concise for typed scalar extraction. JSON_QUERY preserves JSON structure. JSON_TABLE exposes repeated data as rows and lets the rest of SQL handle it.
Exercise
Which function should you use to return the shipping object as JSON, and which should you use to return its city as text?
Show answer
Use JSON_QUERY for the shipping object and JSON_VALUE with the path $.shipping.city for the city text.