Summary: in this tutorial, you will learn about the PostgreSQL JSONB operators and how to use them to process JSONB data effectively.
Introduction to PostgreSQL JSONB operators
JSONB type allows you to store and query JSON data efficiently. JSONB type supports a wide range of operators that help you manipulate and query JSON documents effectively.
The following table illustrates the JSONB operators:
Operator
Syntax
Meaning
->
jsonb->'key'
Extract the value of the ‘key’ from a JSON object as a JSONB value
->>
jsonb->>'key'
Extract the value of the ‘key’ from a JSON object as a text string
@>
jsonb @> jsonb → boolean
Return true if the first JSONB value contains the second JSONB value or false otherwise.
<@
jsonb <@ jsonb → boolean
Return true if the first JSONB value is contained in the second one or false otherwise.
?
jsonb ? text → boolean
Return true if a text string exists as a top-level key of a JSON object or as an element of a JSON array or false otherwise.
?|
jsonb ?| text[] → boolean
Return true if any text string in an array exists as top-level keys of a JSON object or as elements of a JSON array.
?&
jsonb ?& text[] → boolean
Return true if all text strings in an array exist as top-level keys of a JSON object or as elements of a JSON array.
||
jsonb || jsonb → jsonb
Concatenate two JSONB values into one.
-
jsonb - text → jsonb
Delete a key (and its value) from a JSON object, or matching string value(s) from a JSON array.
-
jsonb - text[] → jsonb
Delete all matching keys or array elements from the left operand.
-
jsonb - integer → jsonb
Delete the array element with specified index (negative integers count from the end of the array).
#-
jsonb #- text[] → jsonb
Delete the field or array element at the specified path.
@?
jsonb @? jsonpath → boolean
Return true if a JSON path returns any item for the specified JSONB value.
@@
jsonb @@ jsonpath → boolean
Evaluate a JSON path against a JSONB value and return a boolean result based on whether the JSON path matches any items within the JSONB value
PostgreSQL JSONB operators examples
Let’s set up a sample table and take some examples of using PostgreSQL JSONB operators.
Setting up a table
First, create a table called products that has a JSONB column to store JSON data:
For example, the following statement uses the operator ->> to get the product names as text:
Output:
3) Operator (#>)
The operator #> extracts a JSON object or an element at the specified path:
For example, the following statement uses the operator #> to extract the attributes object from the JSON object in the data column of the products table:
Output:
The following example uses the operator #> to extract the color field of the attributes object from the data column of the products table:
Output:
4) Operator (#>>)
The operator #>> extracts a JSON object or element at a specified path as text:
For example, the following statement uses the operator (#>>) to extract the color from the attributes subobject of the data object as text strings:
Output:
5) Operator @>
The operator @> return true if a JSONB value contains another JSONB value or false otherwise:
For example, the following statement uses the operator @> to retrieve the products in the Electronics category:
Output:
6) Operator <@
The operator <@ returns true if a JSON value is contained within the another JSONB value or false otherwise:
For example:
Output:
7) Operator ||
The operator || concatenates two JSONB values into a single one:
For example, the following statement uses the operator || to concatenate two JSONB values into a single JSONB value:
Output:
In this example, we use the cast operator (::) to convert text strings into JSONB values before concatenating them into a single JSONB value.
8) Operator (?)
The operator ? returns true if a text string exists as a top-level key of a JSON object or as an array element of a JSON array, or false otherwise:
For example, the following statement uses the operator (?) to retrieve the products whose price key exists as the top-level key of the JSON object stored in the data column of the products table:
Output:
The following example uses the operator ? to retrieve all products whose tags have the text Apple:
Output:
9) Operator (?|)
The operator ?| returns true if any elements in a text array exist as top-level keys of a JSON object or as elements of a JSON array, or false otherwise:
For example, the following statement uses the operator ?| to retrieve products whose attributes have either the storage or size keys:
Output:
10) Operator (?&)
The operator ?& returns true if all elements in a text array exist as the top-level keys of a JSON object or as elements of a JSON array, or false otherwise:
For example, the following statement uses the operator ?& to retrieve the products whose attributes have both color or storage keys:
Output:
11) Operator (-)
The operator - allows you to delete a key/value pair from a JSON object or a matching string value from a JSON array:
The following example uses the operator (-) to remove the name key and its value from a JSONB object:
Output:
The following example uses the operator (-) to remove the element "API" from a JSON array:
Output:
12) Operator (-)
The operator - also allows you to delete all matching keys (with their values) from a JSON object or matching elements from a JSON array:
The following example uses the operator (-) to remove the age and email keys and their values from a JSONB object:
Output:
The following example uses the operator (-) to remove the element "API" and "Web Dev" from a JSON array:
Output:
13) Operator (@?)
The operator @? returns true if a JSON path returns any items for the specified JSONB value:
For example, the following uses the @? operator to retrieve the products whose prices are greater than 999:
Output:
In this example, we use the operator @? to check if the JSON path '$.price ? (@ > 999)' returns any element in the JSONB value of the data column.
14) Operator (@@)
The operator (@@) evaluates a JSON path against a JSONB value and returns a boolean result based on whether the JSON path matches any items within the JSONB value. If the result is not a boolean, then the @@ operator returns NULL.
For example, the following example returns null because the JSON path '$.scores' returns an array, not a boolean result:
Output:
However, the following statement returns true because the JSON path '$.scores[*] > 2' matches the elements that are greater than 2.
Output:
Notice that the '$.scores[*] > 2' matches 3, 4, and 5 but it only considers the result of the first matched item, which is 3.