You can use the jsonb_array_elements function to expand a JSONB array into a set of rows, each containing one element of the array. It is a simpler option compared to complex looping logic. It is also more efficient than executing the same operation on the application side by reducing data transfer and processing overhead.
Function signature
jsonb_array_elements example
Suppose you have a table with information about developers:
developers
Now, let's say you want to extract each individual skill from the skills JSON array. You can use jsonb_array_elements for that:
This query returns the following values:
Advanced examples
This section shows advanced jsonb_array_elements examples.
Filtering jsonb_array_elements
You can use the jsonb_array_elements function to extract the sizes from the JSON data and then filter the products based on a specific color (or size):
This query returns the following values:
Handling NULL in jsonb_array_elements
This example updates the table to insert another product (Socks) with one of the values in the sizes as null:
products
Querying for Socks shows how null values in an array are handled:
This query returns the following values:
Ordering json_array_elements output using WITH ORDINALITY
Let's consider a scenario where you have a table named workflow with a JSONB column steps representing sequential steps in a workflow:
workflow
Each workflow consists of a series of tasks, and you want to extract and display the tasks along with their order in the workflow.
This query returns the following values:
Nested arrays in jsonb_array_elements
You can also handle nested arrays with jsonb_array_elements.
Consider a scenario where each product in an electronics_products table has multiple variants, and each variant has an array of sizes and an array of colors.
electronics_products
To handle the nested arrays and extract information about each variant, you can run this query using the jsonb_array_elements function:
This query returns the following values:
jsonb_array_elements with joins
Let's assume you want to retrieve a list of users along with their roles in each organization. The data is stored in an organizations table and a users table.
organizations
users
You can use the jsonb_array_elements function to extract the members from the JSONB array in the organizations table and then join with the users table.
This query returns the following values:
Additional considerations
This section outlines additional considerations including alternative functions.
Alternatives to jsonb_array_elements
Use jsonb_array_elements when you need to maintain the JSON structure of the elements for further JSON-related operations or analysis and jsonb_array_elements_text if you need to work with the extracted elements as plain text for string operations, text analysis, or integration with text-based functions.
If you want to create a comma-separated list of all skills for each developer in the developers table, jsonb_array_elements_text can be used along with string_agg.
This query returns the following values:
Using jsonb_array_elements would result in an error because it returns JSONB values, which cannot be directly concatenated with the string operator.
jsonb_path_query
jsonb_path_query uses JSON Path expressions for flexible navigation and filtering within JSONB structures and returns a JSONB array containing matching elements. It supports filtering within the path expression itself, enabling complex conditions and excels at navigating and extracting elements from nested arrays and objects.
If your query involves navigating through multiple levels of nesting, complex filtering conditions, or updates to JSONB data, jsonb_path_query is often the preferred choice.
Consider a simple example — to extract the first skill of each developer in the developers table: