Sub topic 9
Querying nested data with SQL/JSON path
This chapter assumes you know basic SELECT, WHERE, joins, PostgreSQL data types, and JSON objects and arrays. It also assumes an orders table with a jsonb column named details. SQL/JSON path is useful when a document has several nested levels or when ordinary operators become hard to read.
Why use SQL/JSON path?
A JSON path is a small expression that describes how to walk through a JSON document. PostgreSQL evaluates the path and returns matching values.
A path starts at the document root:
$means the root document..nameselects an object member.[*]selects every element in an array.? (...)filters values.@means the value currently being examined.
Suppose details contains this document:
{
"status": "paid",
"items": [
{"sku": "A1", "qty": 2},
{"sku": "B7", "qty": 1}
]
}The path $.items[*] visits both objects in the items array. The filter ? (@.qty > 1) keeps only the first object. Adding .sku returns its SKU.
The following diagram shows that evaluation.
Returning matching values
The function jsonb_path_query returns one row for every value that matches a path. It returns values as jsonb, so a string result may include JSON string formatting when displayed.
SELECT id,
jsonb_path_query(
details,
'$.items[*] ? (@.qty > 1) .sku'
) AS sku
FROM orders;For Ada's order, this returns the SKU A1. Grace's item has quantity 1, so it produces no result.
Use jsonb_path_query_first when you need only the first match:
SELECT id,
jsonb_path_query_first(
details,
'$.items[*] ? (@.qty > 1) .sku'
) AS first_large_item
FROM orders;Use jsonb_path_query_array when you want all matching values in one JSON array:
SELECT id,
jsonb_path_query_array(
details,
'$.items[*] ? (@.qty > 1) .sku'
) AS large_item_skus
FROM orders;Testing whether a path matches
When you need a yes-or-no condition, use jsonb_path_exists. It returns true if at least one value matches the path.
SELECT id, customer
FROM orders
WHERE jsonb_path_exists(
details,
'$.items[*] ? (@.qty > 1)'
);This query finds orders containing at least one item whose quantity is greater than 1. The path checks the item object itself, so it does not need to return the SKU.
You can also compare text values:
SELECT id, customer
FROM orders
WHERE jsonb_path_exists(
details,
'$ ? (@.status == "paid")'
);The path uses double quotes for the JSON string paid. The complete SQL string uses single quotes.
Missing values and path modes
PostgreSQL normally evaluates JSON paths in lax mode. In this mode, missing members often produce an empty result instead of an error. For example, a document without items simply has no matching item rows.
Strict mode reports structural problems instead:
SELECT jsonb_path_query(
details,
'strict $.items[*].sku'
)
FROM orders;Strict mode can help reveal malformed documents, but it can also make a query fail when optional fields are absent. Choose it when missing structure should be treated as an error.
JSON path is not a replacement for every JSON operator. A direct lookup such as details ->> 'status' is shorter for one known field. Path expressions become more useful when you combine nested arrays, filters, and conditions.
| Need | Suitable function |
|---|---|
| Return every matching value | `jsonb_path_query` |
| Return one matching value | `jsonb_path_query_first` |
| Return matches as one array | `jsonb_path_query_array` |
| Test whether a match exists | `jsonb_path_exists` |
Exercise
Write a query that returns the id of every order containing an item with quantity at least 2.
Show answer
SELECT id FROM orders WHERE jsonbpathexists(details, '$.items[*] ? (@.qty >= 2)');