PostgreSQL 18 reference · JSON Functions

PostgreSQL JSONB_PATH_QUERY() Function

Return every JSON item matched by an SQL/JSON path expression, such as '$.items[*] ? (@.price > 10)'.

PostgreSQL 18 Since 12

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

Syntax

jsonb_path_query(target, path, vars)

Parameters

ParameterDescription
jsonb A jsonb (or json) value.
path An SQL/JSON path (jsonpath).
vars optional Values for $name variables.

Return value

A set of jsonb rows.

Examples

Filter with a path
SELECT jsonb_path_query('{"items":[{"n":"tea","p":3},{"n":"cake","p":12}]}', '$.items[*] ? (@.p > 5).n') AS expensive;

Output

 expensive
-----------
 "cake"
(1 row)

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

Test a path with @?
SELECT '{"items":[{"p":3},{"p":12}]}'::jsonb @? '$.items[*] ? (@.p > 10)' AS any_over_10;

Output

 any_over_10
-------------
 t
(1 row)

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

Version history

VersionChange
PostgreSQL 12Added

Was this page helpful?