> ## Documentation Index
> Fetch the complete documentation index at: https://docs.firebolt.io/llms.txt
> Use this file to discover all available pages before exploring further.

> Reference material for JSON_ARRAY_LENGTH function

# JSON_ARRAY_LENGTH

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

```sql theme={"theme":{"light":"css-variables","dark":"css-variables"}}
JSON_ARRAY_LENGTH(<json>)
```

## Parameters

| Parameter | Description                           | Supported 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:

```sql theme={"theme":{"light":"css-variables","dark":"css-variables"}}
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)
```
