PostgreSQL 18 reference · JSON Functions

PostgreSQL JSONB_ARRAY_LENGTH() Function

Return the number of elements in a JSON array. Raises an error for objects and scalars.

PostgreSQL 18 Since 9.4

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

Syntax

jsonb_array_length(jsonb)

Parameters

ParameterDescription
jsonb A jsonb (or json) value.

Return value

integer.

Examples

Count elements
SELECT jsonb_array_length('[1, 2, 3]') AS n;

Output

 n
---
 3
(1 row)

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

A nested array
SELECT jsonb_array_length('{"tags":["x","y"]}'::jsonb -> 'tags') AS tags;

Output

 tags
------
    2
(1 row)

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

Version history

VersionChange
PostgreSQL 9.4Added

In other languages

Was this page helpful?