PostgreSQL JSON Path
Summary: in this tutorial, you will learn about the PostgreSQL JSON path and how to use it to locate an element within a JSON document.
Introduction to PostgreSQL JSON Path
JSON path is a query language that allows you to locate specific values or elements within a JSON document.
JSON path offers a way to match elements within JSON data, similar to how XPATH
is used for XML documents.
To construct JSON path expressions, you can use the combination of the following operators:
$
– Represent the root element to query..key
– Use a dot (.) followed by a key name (.key
) to access a field of a JSON object or (.*
) to access all properties of a JSON object.[n]
– Use square brackets ([]
) to access an array element by its index (n), or [*] to access all array elements.@
– Represent the current node being processed by a filter predicate.[start: end]
– Array slice operator.[?(expression)]
– Filter expression that evaluates to a boolean value.
To extract specific elements from a JSON path jsonb_path_query()
function:
The function returns all JSON items for the jsonb_data
based on a JSON path.
PostgreSQL JSON Path examples
Let’s take some examples of using JSON paths.
Setting up a sample table
First, create a table called person
that includes a JSONB
column:
Second, insert a new row into the person
table:
Output:
The JSON data in the info
column of the person
table looks like the following:
1) Extracting the name of the person
The following statement uses the jsonb_path_query()
to extract the name of the person:
Output:
In this example, we use the $.name
path to access the value of the name
property of the top-level JSON object.
2) Extracting all values of a JSON object
The following example uses the jsonb_path_query()
function to retrieve all values of the JSON object in the info
column:
Output:
In this example, the $.*
path locates the values of all properties of the top-level JSON object. Therefore, the jsonb_path_query
returns the values of all properties of the JSON object.
3) Extracting array elements
The following example uses the jsonb_path_query()
function to get the name of the first pet:
Output:
In this example, we use the JSON path $.pets[0].name
to locate the name of the first pet:
$
: represents the top-level JSON object.$.pets
locates the values of the property with the namepets
, which is a JSON array.$.pets[0]
returns the first element of the$.pets
array, which is a JSON object.$.pets[0].name
returns the value of the propertyname
of the$.pets[0]
object.
The following example uses the JSON path $.pets[*].name
to return all pet names of a person object:
Output:
The wildcard *
means all elements.
4) Filter JSON
The following example uses a filter expression to find the pet whose species is cat:
Output:
Here’s the break-down of the JSON path expression $.pets[*] ? (@.species == "Cat")
:
$.pets[*]
: selects all elements (*
) within the “pets” array. The$.
denotes the root of the JSON document andpets[*]
represents all array elements of thepets
array.? (@.species == "Cat")
: filters the selected elements from thepets
array. The?
is used to apply the filter condition(@.species == "Cat")
, which checks if the value of thespecies
key in each selected element is equal toCat
.
In short, the JSON path $.pets[*] ? (@.species == "Cat")
matches all objects within the pets
array where the value of the species
key is Cat
.
JSON path mode
PostgreSQL allows you to optionally specify a path mode at the beginning of the JSON path expression:
The mode can be lax
or strict
:
- In
lax
mode, the function returns an empty value (result set) if the JSON path expression has an error. For example, if you use the$.email
path for the JSON document that doesn’t contain theemail
key, the function returns an empty result set. - In
strict
mode, the function issues an error if the path expression contains an error.
The default is lax
mode.
The following statement attempts to extract the email
from the JSON data in the info
column of the person
table:
Output:
It returns no row because the JSON object does not have the email
key.
The following statement extracts the email
key but uses the strict
mode for the JSON path:
Output:
The output shows that the function raises an error.
Summary
- Use JSON paths to locate specific values or elements within a JSON document.
- Use the
jsonb_path_query()
function to return all items within a JSON document that match a specified JSON path.