BigQuery · JSON function
BigQuery JSON_QUERY_ARRAY
JSON_QUERY_ARRAY(json[, json_path]) turns a JSON array into a SQL ARRAY with one element per item: ARRAY<JSON> for JSON input, JSON-formatted strings for STRING input. Combine it with UNNEST to get one row per item of a nested JSON list.
JSON_QUERY_ARRAY is the standard way to turn a JSON array into rows. It returns a SQL ARRAY, so CROSS JOIN UNNEST(JSON_QUERY_ARRAY(payload, '$.items')) AS item produces one row per item, and each item can be read with JSON_VALUE, with dot access, or with typed converters such as INT64(item.qty).
Elements stay JSON: strings keep their quotes and objects stay objects. When the array holds only scalars and you want plain strings, JSON_VALUE_ARRAY returns ARRAY<STRING> directly. The function returns NULL if the path is missing or doesn't point to an array, and an empty array for [].
It replaces the deprecated JSON_EXTRACT_ARRAY. Keys with special characters are escaped with double quotes ('$."line.items"'), the path must be a constant, and lax modes are not accepted; use JSON_QUERY for those. Note that dot access alone doesn't give you an array: UNNEST(payload.items) fails because payload.items is a single JSON value.
Syntax
JSON_QUERY_ARRAY(json_string_expr[, json_path])
JSON_QUERY_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<JSON> for JSON input, ARRAY<STRING> of JSON-formatted text for STRING input. NULL if the path is missing or doesn't point to an array.
Examples
One row per order item from a JSON column
WITH orders AS (
SELECT 'A-100' AS order_id,
JSON '{"items": [{"sku": "KB-1", "qty": 1}, {"sku": "MS-2", "qty": 2}]}' AS payload
)
SELECT
order_id,
JSON_VALUE(item, '$.sku') AS sku,
INT64(item.qty) AS qty
FROM orders
CROSS JOIN UNNEST(JSON_QUERY_ARRAY(payload, '$.items')) AS item
ORDER BY sku;| order_id | sku | qty |
|---|---|---|
| A-100 | KB-1 | 1 |
| A-100 | MS-2 | 2 |
JSON elements and plain strings
SELECT
JSON_QUERY_ARRAY('{"tags": ["sale", "new"]}', '$.tags') AS json_elements,
JSON_VALUE_ARRAY('{"tags": ["sale", "new"]}', '$.tags') AS string_elements;| json_elements | string_elements |
|---|---|
| ["sale", "new"] | [sale, new] |
Count the items in a JSON array
WITH carts AS (
SELECT 'c-1' AS cart_id, '{"items": [{"sku": "KB-1"}, {"sku": "MS-2"}, {"sku": "CB-3"}]}' AS body UNION ALL
SELECT 'c-2', '{"items": []}'
)
SELECT cart_id, ARRAY_LENGTH(JSON_QUERY_ARRAY(body, '$.items')) AS item_count
FROM carts
ORDER BY cart_id;| cart_id | item_count |
|---|---|
| c-1 | 3 |
| c-2 | 0 |
Empty, non-array and missing values
SELECT
JSON_QUERY_ARRAY('{"tags": []}', '$.tags') AS empty_array,
JSON_QUERY_ARRAY('{"tags": "sale"}', '$.tags') IS NULL AS not_an_array,
JSON_QUERY_ARRAY('{}', '$.tags') IS NULL AS missing_key;| empty_array | not_an_array | missing_key |
|---|---|---|
| [] | true | true |
Common mistakes
- Losing parent rows with CROSS JOIN UNNEST when the array is missing or empty. Use
LEFT JOIN UNNEST(...)to keep orders without items. - Comparing elements with plain strings. An element
"sale"includes its quotes; use JSON_VALUE_ARRAY, orJSON_VALUE(item)per element. - Passing a
laxpath. JSON_QUERY_ARRAY accepts only strict paths and fails with "JSONPath must start with '$'". - Unnesting dot access directly.
UNNEST(payload.items)fails with "Values referenced in UNNEST must be arrays"; wrap it asUNNEST(JSON_QUERY_ARRAY(payload.items)).
Cost and performance
- On a JSON column, BigQuery reads only the virtual columns under the requested path, so exploding
$.itemsdoesn't bill the rest of the payload. From a STRING column, the whole string is billed.
FAQ
How do I unnest a JSON array in BigQuery?
CROSS JOIN UNNEST(JSON_QUERY_ARRAY(json_col, '$.items')) AS item, then read fields with JSON_VALUE(item, '$.sku'). Use LEFT JOIN UNNEST to keep rows without items.
What is the difference between JSON_QUERY_ARRAY and JSON_VALUE_ARRAY?
JSON_QUERY_ARRAY keeps each element as JSON and works for arrays of objects. JSON_VALUE_ARRAY returns plain strings and works only for arrays of scalars; it returns NULL if any element is an object or an array.
How do I count the items in a JSON array?
ARRAY_LENGTH(JSON_QUERY_ARRAY(json_col, '$.items')). It returns NULL when the key is missing, so wrap it in IFNULL if you want 0.
Related
More JSON functions
All BigQuery JSON functions · All functions · BigQuery error messages
