Skip to main content
Returns the number of elements in a JSON array. Returns NULL if the input is NULL, and raises an error if the input JSON value is not an array.

Syntax

JSON_ARRAY_LENGTH(<json>)

Parameters

ParameterDescriptionSupported input types
<json>The JSON array whose length is taken.JSON

Return Type

The JSON_ARRAY_LENGTH function returns a result of type INTEGER.

Remarks

Only the top-level elements of the array are counted; nested arrays count as a single element each. An untyped string literal is interpreted as JSON, so an explicit ::JSON cast is not required.

Examples

The following examples return the number of elements in a JSON array:
SELECT JSON_ARRAY_LENGTH('[1, 2, 3]'::JSON);        -- Returns 3
SELECT JSON_ARRAY_LENGTH('[1, 2, 3]');              -- Returns 3 (untyped literal interpreted as JSON)
SELECT JSON_ARRAY_LENGTH('[]'::JSON);               -- Returns 0
SELECT JSON_ARRAY_LENGTH('[[1, 2], [3], 4]'::JSON); -- Returns 3 (top-level elements only)