PostgreSQL JSON Extract
Summary: in this tutorial, you will learn how to use the operator ->
and ->>
to extract an element from a JSON array or a value of a key from a JSON object.
Extracting elements from JSON arrays
To extract an element of a JSON array as a JSONB
value, you use the ->
operator.
Here’s the syntax for using the ->
operator:
In this syntax, n
locates the nth element in a JSON array. n can be positive or negative. If the n is negative, the operator ->
returns the element from the end of the array.
Note that the first element has an index of zero and the last element has an index of -1.
If the nth element does not exist, the operator ->
returns null
. To extract an array element as a text string, you can use the ->>
operator:
Extracting JSON array element examples
Let’s explore some examples of using the ->
and ->>
operators.
1) Setting up a sample table
First, create a new table called employees
to store employee data:
Second, insert some rows into the employees
table:
Output:
2) Extracting the first array element example
The following example uses the -> operator to retrieve the first phone number of an employee with the name John Doe:
Output:
In this example, we use the ->
operator with the index 0. Therefore, the expression phones -> 0
returns the first element in the phones
array as a JSONB
value.
To extract the first phone number as a text string, you can use the ->> operator:
Output:
3) Extracting the last array element example
The following example uses the ->
operator to retrieve the first phone number of an employee with the name Jane Smith
:
Output:
To extract the last phone number as a JSONB
value, you can use the ->> operator:
Output:
4) Extracting an element that does not exist
The following example uses the ->
operator to retrieve the 4th phone number of an employee with the name Jane Smith
:
Output:
Since Jane Smith has 3 phone numbers only, the query returns NULL
.
Extracting object value
To extract a value of a JSON object by a key, you use the -> operator:
The -> operator returns the value of the ‘key’ as a JSONB value. If the key does not exist, the -> operator returns null.
If you want to return the value as an SQL value, you can use the ->> operator:
Extracting JSON object value example
1) Setting up a sample table
First, create a new table called requests
:
Second, insert some rows into the requests
table:
Output:
2) Extract a value from a JSON object
The following example uses the ->
operator to extract the current position of the request of employee ID 1:
Output:
The return value is a JSONB value.
To get the current position as a text string, you can use the ->>
operator:
Output:
2) Extract a key that does not exist
The following example attempts to extract a value of a non-existing key from a JSON object:
Output:
Summary
- Use the
json_array -> n
andjson_array ->> n
operator to extract a JSON array element as aJSONB
value or as a text string specified by an index. - Use the
json_object -> 'key'
andjson_object ->> 'key'
operator to extract a value from an object specified by a key as a JSONB value and a text string.