This feature is in and subject to change. To share feedback and/or issues, contact Support.
JSONPath expression
A JSONPath expression consists of an optional mode (lax or strict), followed by a scalar expression (such as 1 + 2), a predicate expression (such as 1 != 2 or exists($)), or a path-based expression rooted at $. A path-based expression is composed of one or more accessor operators that are optionally interleaved with one or more filter expressions introduced by ?. Expressions can optionally include scalar expressions, predicate operators for conditional logic, and a method applied to the current value. The path is evaluated left to right, and each stage refines or filters the result.
Variables
Use the following variables in path and filter expressions to reference parts of the JSON document or external values.| Variable | Description | Example usage |
|---|---|---|
$ | Root of the JSON document. | $.players[0] |
@ | Current item being evaluated in a filter expression. | @.team == "Lakers" |
$var | A named variable defined in the var argument. | @.price > $min (with {"min": 100}) |
Operators
Accessor operators
Use the following operators in path expressions to navigate JSON objects and arrays.| Operator | Description | Example usage |
|---|---|---|
.key | Access a named field key in a JSON object. | $.players |
.* | Access all fields in the current object. | $.stats.* |
.key[a] | Access a specific a element in an array field. | $.players[0] |
.key[a to b] | Access an index range from a to b in an array field. | $.players[0 to 3] |
.key[last] | Access the last index element in an array field. | $.players[last] |
.key[*] | Access all elements in an array field. | $.players[*] |
.$var | Access the field named by a variable var. | $.players[0].$field (with $field = "name") |
Predicate operators
Use the following operators in predicate check expressions to compare values, evaluate conditions, and combine logical clauses.| Operator | Description | Example usage | ||||
|---|---|---|---|---|---|---|
== | Equality | @.team == "Lakers" | ||||
!= | Inequality | @.name != "Luka" | ||||
> | Greater than | @.stats.ppg > 25 | ||||
< | Less than | @.stats.ppg < 30 | ||||
>= | Greater than or equal to | @.stats.rpg >= 10 | ||||
<= | Less than or equal to | @.stats.apg <= 4 | ||||
starts with | String prefix match | @.name starts with "A" | ||||
like\_regex | Regex string match | @.name like\_regex "^L.\*" | ||||
is unknown | True if expression evaluates to null | (@.age > 25) is unknown | ||||
! | Logical NOT | !(@.team == "Mavericks") | ||||
&& | Logical AND | @.ppg > 20 && @.team == "Lakers" | ||||
| ` | ` | Logical OR | `@.ppg > 20 | @.team == “Lakers”` |
Methods
Append the following methods to a path (after.) to access or transform the value. For examples, refer to Access using methods.
| Method | Description | Example usage |
|---|---|---|
size() | Returns the size of an array, or 1 for a scalar. | $.players.size() |
type() | Returns the type of the current value as a string. | $.players[0].stats.type() |
abs() | Returns the absolute value of a number. | $.players[0].stats.ppg.abs() |
floor() | Returns the nearest integer less than or equal to the current value. | $.players[1].stats.ppg.floor() |
ceiling() | Returns the nearest integer greater than or equal to the current value. | $.players[1].stats.ppg.ceiling() |
JSONPath functions
Use JSONPath functions to extract or evaluate targetJSONB data according to a specified path. For full details on JSONPath functions, refer to .
| Function | Description | If no match |
|---|---|---|
jsonb_path_exists(jsonb, jsonpath) | Returns true if any match is found. | false |
jsonb_path_match(jsonb, jsonpath) | Returns true if the path expression evaluates to true. Only useful with predicate check expressions, as it expects a single Boolean value. | false |
jsonb_path_query(jsonb, jsonpath) | Returns all matches as a set of JSONB values. | NULL |
jsonb_path_query_array(jsonb, jsonpath) | Returns all matches as a single JSONB array. | [] |
jsonb_path_query_first(jsonb, jsonpath) | Returns the first match only. | NULL |
target(required): A value to access.path(required): A JSONPath expression.vars(optional): A value referenced as a variable in the path.silent(optional): A Boolean value that specifies whether to throw errors during execution. If not specified, this isfalse.
Example setup
To follow the examples on this page, create and populate the following table:JSONB structure.
Access JSONB content
Access JSONB content by passing a JSONPath expression to a JSONPath function.
Access entire document
To return the entireJSONB value, query the root accessor ($):
Access JSONB fields
To return a specific field in the JSONB value, query its corresponding key accessor.
The following query returns the season value from the root object:
stats value for each element in the players array, using the array accessor:
ppg value within the stats array for each player:
Access JSONB array elements
Access JSONB array elements through their index value (JSONB arrays are 0-indexed), the * (any element) keyword, or the last (last element) keyword.
The following query returns the name value of the first element in the players array (i.e., the first player’s name):
last keyword:
Access using methods
You can use JSONPath methods to access or transform data in the path. The following query returns the type of each value in theplayers array, using the type() method:
players array, using the size() method:
ppg statistic for each player, using the floor() method:
Check expressions
A JSONPath expression can be a predicate check expression that returns a Boolean value. Use one or more predicate operators to specify conditions such as equality, logical expressions, and existence. Each of the following check expressions evaluates to true:The preceding check expressions can be used with the
jsonb_path_match function for a similar result.Filter expressions
A filter expression uses a predicate check expression to returnJSONB fields that match a condition.
To write a filter expression, insert ? into a JSONPath expression, followed by a check expression that uses @ to reference each item selected by the preceding path step. The filter expression only returns items that evaluate to true.
For example, the following JSONPath expression selects all elements in an items array ($.items[*]), filters all items whose price value is greater than 100 ((@.price > 100)), and returns the name value for each filtered item (.name):
Filter with comparison operators
The following query returns all players who averaged more than 25 points per game:rpg statistic to the preceding filter:
jsonb_path_exists function. The following query evaluates whether any player averaged more than 25 points per game:
Filter with string matching
The following two queries use thestarts with and like_regex operators to return the same result:
Variables in JSONPath expressions
Define a variable in a JSONPath expression by prefixing it with$. Then specify a value in the vars argument of the JSONPath function.
The following query filters players whose ppg is greater than the value of the min variable:
Arithmetic operations
JSONPath expressions can include arithmetic:Control function output
Return first match
Use thejsonb_path_query_first function to return the first query result.
The following query returns the first name in the players array:
Return all matches as array
Use thejsonb_path_query_array function to return all query results as a JSONB array.
The following query returns all team values in a single array.
Structural error handling
By default, JSONPath expressions are evaluated inlax mode, which tolerates structural mismatches between the path and the JSONB data:
-
When the path contains keys that are missing in the
JSONBstructure,NULLorfalsevalues are returned, depending on the function, rather than an error. -
Values are automatically wrapped into arrays or unwrapped from arrays. For example, the following query works without an array accessor because it unwraps the
playersarray:
strict at the start of the path expression. The following query throws an error because it requires an explicit array accessor for players (e.g., players[*]):
silent to true in a JSONPath function suppresses any errors:
Order of evaluation with parentheses
A JSONPath expression is normally evaluated left to right, while respecting operator precedence. For example, multiplication takes precedence over addition:Known limitations
- The following keywords are only accepted in lowercase:
strict,lax,exists,like_regex,flag,is unknown,to,last. - Comparisons involving empty arrays (e.g.,
SELECT jsonb_path_query('{"a": [1], "b": []}', '$.a == $.b');) returnnull, rather thanfalseas in PostgreSQL.

