BigQuery · JSON function
BigQuery JSON_VALUE_ARRAY
JSON_VALUE_ARRAY(json[, json_path]) extracts a JSON array of scalars as an ARRAY<STRING> with the quotes removed. Use it for tag lists, IDs and flags stored in JSON; it returns NULL if any element is an object or an array.
JSON_VALUE_ARRAY is the array counterpart of JSON_VALUE: {"tags": ["sale", "new"]} with the path '$.tags' gives [sale, new] as ARRAY<STRING>. Numbers and booleans come back as text, so cast the elements when you need numbers. It replaces the deprecated JSON_EXTRACT_STRING_ARRAY.
It handles arrays of scalars only. If any element is an object or an array, the whole result is NULL; use JSON_QUERY_ARRAY for those. A missing path or a non-array value also returns NULL, and [] returns an empty array.
JSON null elements become SQL NULL elements. That is fine inside the query, but an array that reaches the result with a NULL in it fails with Array cannot have a null element. Filter the nulls out in an ARRAY subquery, or test membership directly with 'sale' IN UNNEST(JSON_VALUE_ARRAY(...)).
Syntax
JSON_VALUE_ARRAY(json_string_expr[, json_path])
JSON_VALUE_ARRAY(json_expr[, json_path])Arguments and return value
| Argument | Description |
|---|---|
json_string_expr | JSON-formatted text in a STRING. |
json_expr | A value of type JSON. |
json_path | Optional constant JSONPath to the array, default $. Escape special keys with double quotes. |
Returns: ARRAY<STRING>. NULL if the path is missing, isn't an array, or the array contains objects or arrays.
Examples
Tags and IDs as plain strings
SELECT
JSON_VALUE_ARRAY('{"tags": ["sale", "new", "eco"]}', '$.tags') AS tags,
JSON_VALUE_ARRAY(JSON '{"ids": [101, 102]}', '$.ids') AS ids_as_strings;| tags | ids_as_strings |
|---|---|
| [sale, new, eco] | [101, 102] |
Rows whose JSON array contains a value
P3 has no tags key. Its NULL array makes the IN test false, so the row is filtered out.
WITH products AS (
SELECT 'P1' AS sku, JSON '{"tags": ["sale", "new"]}' AS attrs UNION ALL
SELECT 'P2', JSON '{"tags": ["new"]}' UNION ALL
SELECT 'P3', JSON '{}'
)
SELECT sku
FROM products
WHERE 'sale' IN UNNEST(JSON_VALUE_ARRAY(attrs, '$.tags'))
ORDER BY sku;| sku |
|---|
| P1 |
Convert to an INT64 array
SELECT
ARRAY(
SELECT CAST(id AS INT64)
FROM UNNEST(JSON_VALUE_ARRAY('{"ids": [101, 102, 205]}', '$.ids')) AS id WITH OFFSET AS pos
ORDER BY pos
) AS ids;| ids |
|---|
| [101, 102, 205] |
Objects give NULL; null elements must be filtered
Returning the tags array as is would fail, because its JSON null becomes a NULL element.
SELECT
JSON_VALUE_ARRAY('{"items": [{"sku": "KB-1"}]}', '$.items') IS NULL AS objects_give_null,
ARRAY(
SELECT tag
FROM UNNEST(JSON_VALUE_ARRAY('{"tags": ["sale", null, "new"]}', '$.tags')) AS tag WITH OFFSET AS pos
WHERE tag IS NOT NULL
ORDER BY pos
) AS tags_without_nulls;| objects_give_null | tags_without_nulls |
|---|---|
| true | [sale, new] |
Common mistakes
- Using it on arrays of objects. The result is NULL, not an error; use JSON_QUERY_ARRAY.
- Returning arrays that contain JSON nulls. The query fails at output with "Array cannot have a null element"; drop the nulls in an ARRAY subquery.
- Forgetting that elements are STRING.
101 IN UNNEST(JSON_VALUE_ARRAY(...))fails with "No matching signature for operator IN UNNEST"; compare with'101'or cast the elements. - Using
$['a.b']escaping, which only the deprecated JSON_EXTRACT functions accept.
FAQ
How do I convert a JSON array to a BigQuery array?
For scalars, JSON_VALUE_ARRAY(json_col, '$.tags') returns ARRAY<STRING>. For objects, JSON_QUERY_ARRAY(json_col, '$.items') returns one JSON element per object.
How do I check if a JSON array contains a value?
WHERE 'sale' IN UNNEST(JSON_VALUE_ARRAY(attrs, '$.tags')). Rows without the key produce a NULL array, and IN UNNEST over it is false.
What is the difference between JSON_VALUE_ARRAY and JSON_EXTRACT_STRING_ARRAY?
They return the same arrays. JSON_VALUE_ARRAY is the recommended function and escapes special keys with double quotes; JSON_EXTRACT_STRING_ARRAY is deprecated and uses $['key'].
Related
Errors you may hit
More JSON functions
All BigQuery JSON functions · All functions · BigQuery error messages
