PostgreSQL jsonb_array_elements() Function

The PostgreSQL jsonb_array_elements() function expands the top-level JSONB array into a set of JSONB values.

jsonb_array_elements() Syntax

This is the syntax of the PostgreSQL jsonb_array_elements() function:

jsonb_array_elements(any_array JSONB) -> SETOF JSONB

Parameters

any_array

Required. A JSONB array.

Return value

The PostgreSQL jsonb_array_elements() function returns a set including all the top-level elements in the JSONB array specified by the parameter.

jsonb_array_elements() Examples

This example shows how to use the PostgreSQL jsonb_array_elements() function to expand a JSONB array into a set of JSONB values.

SELECT jsonb_array_elements('[1, 2, [3, 4]]');
 jsonb_array_elements
---------------------
 1
 2
 [3, 4]

Since the jsonb_array_elements() function return value is of type SETOF, you can use jsonb_array_elements() as a temporary table in the SELECT * FROM statement:

SELECT * FROM jsonb_array_elements('[1, 2, [3, 4]]');
 value
--------
 1
 2
 [3, 4]