Menu
Amazon Redshift
Database Developer Guide (API Version 2012-12-01)

JSON_ARRAY_LENGTH Function

JSON_ARRAY_LENGTH returns the number of elements in the outer array of a JSON string. If the null_if_invalid argument is set to true and the JSON string is invalid, the function returns NULL instead of returning an error.

For more information, see JSON Functions.

Syntax

Copy
json_array_length('json_array' [, null_if_invalid ] )

Arguments

json_array

A properly formatted JSON array.

null_if_invalid

A Boolean value that specifies whether to return NULL if the input JSON string is invalid instead of returning an error. To return NULL if the JSON is invalid, specify true (t). To return an error if the JSON is invalid, specify false (f). The default is false.

Return Type

INTEGER

Example

The following example returns the number of elements in the array:

Copy
select json_array_length('[11,12,13,{"f1":21,"f2":[25,26]},14]'); json_array_length ----------------- 5

The following example returns an error because the JSON is invalid.

Copy
select json_array_length('[11,12,13,{"f1":21,"f2":[25,26]},14'); An error occurred when executing the SQL command: select json_array_length('[11,12,13,{"f1":21,"f2":[25,26]},14')

The following example sets null_if_invalid to true, so the statement the returns NULL instead of returning an error for invalid JSON.

Copy
select json_array_length('[11,12,13,{"f1":21,"f2":[25,26]},14',true); json_array_length -----------------