BigQuery · JSON function
BigQuery JSON_EXTRACT_ARRAY
JSON_EXTRACT_ARRAY(json[, json_path]) converts a JSON array into a SQL ARRAY with one element per item, as JSON-formatted strings (or JSON values for JSON input), ready for UNNEST. It is deprecated; JSON_QUERY_ARRAY returns the same result.
A JSON array inside a STRING is opaque to SQL until you convert it. JSON_EXTRACT_ARRAY does that: with the path '$.items', the items list becomes an ARRAY whose elements are each item's JSON text. Unnest it to get one row per item, then read fields from each element with JSON_EXTRACT_SCALAR or JSON_VALUE.
Elements keep their JSON form, so string items keep their quotes (["sale", "new"]). For an array of scalars where you want plain strings, use JSON_VALUE_ARRAY, or the deprecated JSON_EXTRACT_STRING_ARRAY.
The function returns NULL when the path is missing, when it points to something that isn't an array, or when the text can't be parsed, and an empty array for []. Google marks it deprecated in favor of JSON_QUERY_ARRAY, which differs only in key escaping: $['a.b'] here, $."a.b" there.
Syntax
JSON_EXTRACT_ARRAY(json_string_expr[, json_path])
JSON_EXTRACT_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 $ (the whole document). |
Returns: ARRAY<STRING> of JSON-formatted text for STRING input; ARRAY<JSON> for JSON input. NULL if the path is missing or doesn't point to an array.
Examples
One row per order item
WITH orders AS (
SELECT 'A-100' AS order_id,
'{"items": [{"sku": "KB-1", "qty": 1}, {"sku": "MS-2", "qty": 2}]}' AS payload
)
SELECT
order_id,
JSON_EXTRACT_SCALAR(item, '$.sku') AS sku,
CAST(JSON_EXTRACT_SCALAR(item, '$.qty') AS INT64) AS qty
FROM orders
CROSS JOIN UNNEST(JSON_EXTRACT_ARRAY(payload, '$.items')) AS item
ORDER BY sku;| order_id | sku | qty |
|---|---|---|
| A-100 | KB-1 | 1 |
| A-100 | MS-2 | 2 |
Elements keep their JSON quotes
JSON_EXTRACT_STRING_ARRAY, also deprecated, strips them.
SELECT
JSON_EXTRACT_ARRAY('{"tags": ["sale", "new"]}', '$.tags') AS json_elements,
JSON_EXTRACT_STRING_ARRAY('{"tags": ["sale", "new"]}', '$.tags') AS plain_strings;| json_elements | plain_strings |
|---|---|
| ["sale", "new"] | [sale, new] |
Empty, non-array, missing and truncated input
SELECT
JSON_EXTRACT_ARRAY('{"tags": []}', '$.tags') AS empty_array,
JSON_EXTRACT_ARRAY('{"tags": "sale"}', '$.tags') IS NULL AS not_an_array,
JSON_EXTRACT_ARRAY('{}', '$.tags') IS NULL AS missing_key,
JSON_EXTRACT_ARRAY('{"tags": ["sale"', '$.tags') IS NULL AS truncated;| empty_array | not_an_array | missing_key | truncated |
|---|---|---|---|
| [] | true | true | true |
Migrating to JSON_QUERY_ARRAY
Without a path, the whole document must be the array.
SELECT
JSON_EXTRACT_ARRAY('[1, 2, 3]') AS whole_document,
JSON_QUERY_ARRAY('{"a.b": ["x"]}', '$."a.b"') AS standard,
JSON_EXTRACT_ARRAY('{"a.b": ["x"]}', "$['a.b']") AS legacy;| whole_document | standard | legacy |
|---|---|---|
| [1, 2, 3] | ["x"] | ["x"] |
Common mistakes
- Expecting plain strings. Elements are JSON text, so
"sale"keeps its quotes andtag = 'sale'never matches; use JSON_VALUE_ARRAY for scalar arrays or JSON_VALUE on each element. - Losing parent rows with
CROSS JOIN UNNEST(...). Orders whose items key is missing or empty produce no rows; useLEFT JOIN UNNEST(...)to keep them. - Using double-quote escaping.
$."a.b"is JSON_QUERY_ARRAY syntax; here write$['a.b']. - Casting elements straight to numbers.
CAST(item AS INT64)works for5but fails with Bad int64 value for a quoted"5"; extract with JSON_EXTRACT_SCALAR first.
Cost and performance
- Pulling an array out of a STRING column bills the entire column. On a JSON column,
JSON_QUERY_ARRAY(payload, '$.items')reads only the virtual columns under the items path.
FAQ
How do I unnest a JSON array in BigQuery?
FROM t CROSS JOIN UNNEST(JSON_QUERY_ARRAY(t.body, '$.items')) AS item, then read fields with JSON_VALUE(item, '$.sku'). JSON_EXTRACT_ARRAY works the same way in older code.
What is the difference between JSON_EXTRACT_ARRAY and JSON_EXTRACT_STRING_ARRAY?
JSON_EXTRACT_ARRAY keeps each element as JSON, so strings keep their quotes and objects stay objects. JSON_EXTRACT_STRING_ARRAY returns plain strings and only works on arrays of scalars. Both are deprecated in favor of JSON_QUERY_ARRAY and JSON_VALUE_ARRAY.
Is JSON_EXTRACT_ARRAY deprecated?
Yes. The GoogleSQL reference recommends JSON_QUERY_ARRAY, which returns the same arrays and escapes special keys with double quotes.
Related
Errors you may hit
More JSON functions
All BigQuery JSON functions · All functions · BigQuery error messages
