← Back to PostgreSQL, page 2

postgresql-0034 · Medium mid

Query nested JSONB with SQL/JSON path expressions: jsonb_path_query, @? and @@.

A jsonpath such as '$.items[*] ? (@.qty > 2)' navigates and filters inside a document. jsonb_path_query returns each match as a row, @? tests whether any match exists, and @@ evaluates a path predicate, with variables passed safely through the vars argument.

Full answer

Schema and sample rows:

sql
CREATE TABLE carts (id int, doc jsonb);
INSERT INTO carts VALUES
  (1, '{"user": "asha", "items": [{"sku": "PEN", "qty": 3, "price": 10}, {"sku": "INK", "qty": 1, "price": 45}]}'),
  (2, '{"user": "ben", "items": [{"sku": "PAD", "qty": 1, "price": 60}]}'),
  (3, '{"user": "chen", "items": []}');
sql
SELECT id, jsonb_path_query(doc, '$.items[*] ? (@.qty > 2).sku') AS bulk_sku FROM carts ORDER BY id;

PostgreSQL returns:

text
 id | bulk_sku
----+----------
 1  | "PEN"
(1 row)
sql
SELECT id, doc @? '$.items[*] ? (@.price >= 50)' AS has_premium, doc @@ '$.items.size() == 0' AS empty FROM carts ORDER BY id;

Result on PostgreSQL 17:

text
 id | has_premium | empty
----+-------------+-------
 1  | f           | f
 2  | t           | f
 3  | f           | t
(3 rows)
sql
SELECT id, jsonb_path_query_array(doc, '$.items[*] ? (@.price > $min).sku', '{"min": 20}') AS above_min FROM carts ORDER BY id;

Result:

text
 id | above_min
----+-----------
 1  | ["INK"]
 2  | ["PAD"]
 3  | []
(3 rows)

Only Asha's cart has an item with quantity above 2, so jsonb_path_query returns one row, the JSON string "PEN". The @? test finds Ben's 60-rupee pad, and @@ evaluates the size check, which is true only for the empty cart. The third query passes the threshold as a variable, avoiding string-built paths, and collects matches into an array per cart, giving an empty array where nothing matches. Path queries with @? and @@ can use a GIN index on the column.

sql
CREATE TABLE carts (id int, doc jsonb);
INSERT INTO carts VALUES
  (1, '{"user": "asha", "items": [{"sku": "PEN", "qty": 3, "price": 10}, {"sku": "INK", "qty": 1, "price": 45}]}'),
  (2, '{"user": "ben", "items": [{"sku": "PAD", "qty": 1, "price": 60}]}'),
  (3, '{"user": "chen", "items": []}');

SELECT id, jsonb_path_query(doc, '$.items[*] ? (@.qty > 2).sku') AS bulk_sku FROM carts ORDER BY id;

SELECT id, doc @? '$.items[*] ? (@.price >= 50)' AS has_premium, doc @@ '$.items.size() == 0' AS empty FROM carts ORDER BY id;

SELECT id, jsonb_path_query_array(doc, '$.items[*] ? (@.price > $min).sku', '{"min": 20}') AS above_min FROM carts ORDER BY id;

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

Key points

  • jsonpath navigates and filters documents
  • jsonb_path_query returns rows
  • @? tests existence, @@ evaluates predicates
  • Pass variables via the vars argument
  • GIN indexes support @? and @@

Common mistakes

  • Building path strings from user input
  • Unnesting arrays by hand for simple filters

Follow-ups

  • What do lax and strict modes change?
  • How would you index for @? queries?

Tips

  • Use vars for parameters