PostgreSQL 18 reference · JSON Functions

PostgreSQL JSONB_ARRAY_ELEMENTS() Function

Expand a JSON array into one row per element. jsonb_array_elements_text() returns the elements as text.

PostgreSQL 18 Since 9.4

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

SELECT * FROM jsonb_array_elements(jsonb)  |  jsonb_array_elements_text(jsonb)

Parameters

ParameterDescription
jsonb A jsonb (or json) value.

Return value

A set of jsonb (or text) rows.

Examples

Elements as rows
SELECT value FROM jsonb_array_elements('[1, "two", {"three":3}]');

Output

    value
--------------
 1
 "two"
 {"three": 3}
(3 rows)

Run this on your computer — see “Get Started” for setup.

Count tags across rows
WITH posts(id, tags) AS (VALUES (1, '["sql","json"]'::jsonb), (2, '["sql"]'))
SELECT tag, count(*) AS posts
FROM posts, jsonb_array_elements_text(tags) AS tag
GROUP BY tag
ORDER BY tag;

Output

 tag  | posts
------+-------
 json |     1
 sql  |     2
(2 rows)

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
PostgreSQL 9.4Added

In other languages

Was this page helpful?