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
Syntax
jsonb_array_length(jsonb)
Parameters
| Parameter | Description |
|---|---|
jsonb |
A jsonb (or json) value. |
Return value
integer.
Examples
SELECT jsonb_array_length('[1, 2, 3]') AS n;
Output
n --- 3 (1 row)
Run this on your computer — see “Get Started” for setup.
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
| Version | Change |
|---|---|
| PostgreSQL 9.4 | Added |
In other languages
JSON_ARRAY_LENGTH()· SQLite
Related
JSONB_ARRAY_ELEMENTS()· Expand a JSON array into one row per element. jsonb_array_elements_text() returns the elements as text.JSONB_TYPEOF()· Return the type of the top-level JSON value: object, array, string, number, boolean or null.CARDINALITY()· Return the total number of elements in an array across all dimensions, and 0 for an empty array.- SQL String Functions · tutorial