# JSON_ARRAY_LENGTH

> JSON_ARRAY_LENGTH function in StackQL: the number of elements in a JSON array, at the top level or at a path within a JSON value.

Source: https://stackql.io/language-spec/functions/json/json_array_length

Returns the number of elements in a JSON array.  

See also:  
[[` SELECT `]](/language-spec/select) [[` DESCRIBE `]](/language-spec/describe) [[` JSON_EXTRACT `]](/language-spec/functions/json/json_extract) [[ Data Types ]](/language-spec/data-types)

* * * 

:::tip 

Use the [**DESCRIBE**](/language-spec/describe) function to locate `array` or `object` datatypes which can be used with StackQL JSON functions. Use the [**JSON_TYPE**](/language-spec/functions/json/json_type) function to resolve the datatypes of nested objects or array elements.

:::

## Syntax

```sql
SELECT JSON_ARRAY_LENGTH(json_array_or_object [, json_path]) FROM <multipartIdentifier>;
```

## Arguments

__*json_array_or_object*__  
A JSON array or object.

> If *json_array_or_object* is a JSON object (not an array) and *json_path* is not specified then `0` is returned.

__*json_path*__  
The optional JSON path to an array contained within an object.  See [jsonpath.com](https://jsonpath.com/).

> If *json_path* is supplied then the `json_array_length` function locates the path within *json_array_or_object* and returns the length of the array or `0` if *json_path* is an object (not an array).  If *json_path* cannot be located within *json_array_or_object*, then `NULL` is returned.

> If *json_array_or_object* or *json_path* are not JSON objects or are not well-formed, then an error is thrown.

## Return Value(s)
Returns an integer.

* * *

## Examples

### Return the number of disks attached to Google Compute Engine instances

```sql
SELECT name, json_array_length(disks) as num_disks
FROM google.compute.instances 
WHERE project = 'stackql-demo' 
AND zone = 'australia-southeast1-a';
```

### Return the number of network interfaces attached to Google Compute Engine instances

```sql
SELECT name, json_array_length(networkInterfaces) as num_nics
FROM google.compute.instances 
WHERE project = 'stackql-demo' 
AND zone = 'australia-southeast1-a';
```

### Return the number of ACLs associated with Google Storage Buckets

```sql
SELECT name, json_array_length(acl) as num_acls
FROM google.storage.buckets 
WHERE project = 'stackql';
```

### Return the number of scopes associated with a service account

```sql
SELECT name, json_array_length(serviceAccounts, '$[0].scopes') as num_scopes 
FROM google.compute.instances 
WHERE project = 'stackql-demo' 
AND zone = 'australia-southeast1-a';
```

For more information, see [https://www.sqlite.org/json1.html#jarraylen](https://www.sqlite.org/json1.html#jarraylen)
