BigQuery · JSON function
BigQuery JSON_QUERY
JSON_QUERY(json, json_path) returns the JSON value at a path (object, array or scalar) as JSON text for STRING input or as JSON for JSON input, keeping quotes on strings. Use it to pull nested objects and arrays; use JSON_VALUE for plain scalars.
JSON_QUERY is the standard extractor for JSON fragments. With STRING input it returns JSON-formatted text ([{"sku":"KB-1"}], "A-100"); with JSON input it returns a JSON value that you can read further or pass to other JSON functions. On a JSON column, dot access is shorthand for it: payload.user equals JSON_QUERY(payload, '$.user').
It replaces the deprecated JSON_EXTRACT. Keys with special characters are escaped with double quotes ('$."utm.source"'), and the path must be a constant.
JSON_QUERY is the only extractor that accepts a mode prefix. The default, strict, needs the path to match the structure exactly, so $.items.sku returns NULL when items is an array. lax $.items.sku unwraps the array and returns every sku, and lax recursive also unwraps arrays nested in arrays.
How a JSON null comes back depends on the input type: SQL NULL for STRING input, a JSON null (which is not SQL NULL) for JSON input. A missing key is SQL NULL in both cases.
Syntax
JSON_QUERY(json_string_expr, json_path)
JSON_QUERY(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 | A constant JSONPath, optionally prefixed with lax or lax recursive. Escape special keys with double quotes. |
Returns: A JSON-formatted STRING for STRING input, JSON for JSON input. SQL NULL when the path doesn't match.
Examples
Arrays, objects and quoted strings
SELECT
JSON_QUERY('{"order": {"id": "A-100", "items": [{"sku": "KB-1"}]}}', '$.order.items') AS items,
JSON_QUERY('{"order": {"id": "A-100", "items": [{"sku": "KB-1"}]}}', '$.order.id') AS order_id,
JSON_VALUE('{"order": {"id": "A-100", "items": [{"sku": "KB-1"}]}}', '$.order.id') AS order_id_value;| items | order_id | order_id_value |
|---|---|---|
| [{"sku":"KB-1"}] | "A-100" | A-100 |
Collect a field from every array element with lax
In the default strict mode, $.items.sku returns NULL because items is an array. lax unwraps it.
SELECT JSON_QUERY(
JSON '{"items": [{"sku": "KB-1", "qty": 1}, {"sku": "MS-2", "qty": 2}]}',
'lax $.items.sku'
) AS all_skus;| all_skus |
|---|
| ["KB-1","MS-2"] |
Dot access, JSON null and SQL NULL
On JSON input, a JSON null comes back as JSON null, so IS NULL is false.
WITH t AS (
SELECT JSON '{"user": {"id": 42, "plan": null}}' AS payload
)
SELECT
JSON_QUERY(payload, '$.user') AS via_function,
payload.user AS via_field_access,
JSON_QUERY(payload, '$.user.plan') AS plan,
JSON_QUERY(payload, '$.user.plan') IS NULL AS plan_is_sql_null
FROM t;| via_function | via_field_access | plan | plan_is_sql_null |
|---|---|---|---|
| {"id":42,"plan":null} | {"id":42,"plan":null} | null | false |
Escaping a key that contains a dot
SELECT
JSON_QUERY('{"utm.source": "newsletter"}', '$."utm.source"') AS escaped_key,
JSON_QUERY('{"utm.source": "newsletter"}', '$.utm.source') AS unescaped_key;| escaped_key | unescaped_key |
|---|---|
| "newsletter" | NULL |
Common mistakes
- Comparing the output with plain text. A string comes back as
"paid", quotes included; compare with JSON_VALUE instead. - Grouping or sorting by a JSON result. It fails with "Grouping by expressions of type JSON is not allowed"; group by JSON_VALUE of the field instead.
- Expecting arrays to be unwrapped. Strict mode returns NULL for
$.items.sku; use$.items[0].sku,lax $.items.sku, or JSON_QUERY_ARRAY with UNNEST. - Testing a JSON null with IS NULL. On JSON input,
JSON_QUERY(payload, '$.coupon') IS NULLis false for{"coupon": null}; checkJSON_TYPE(...) = 'null'. - Using JSON_EXTRACT's single-quote escaping
$['a.b'], which JSON_QUERY rejects.
Cost and performance
- Dot access and JSON_QUERY on a JSON column read only the paths you reference, while the same extraction from a STRING column bills the entire string. For JSON columns the dry-run estimate shows the full column size, so the bytes actually billed can be lower than the estimate.
FAQ
What is the difference between JSON_QUERY and JSON_VALUE?
JSON_QUERY returns any JSON value (objects, arrays or scalars) as JSON, with quotes kept on strings. JSON_VALUE returns only scalars, as a plain STRING, and NULL for objects and arrays.
What is the difference between JSON_QUERY and JSON_EXTRACT?
They return the same values. JSON_QUERY is the recommended function, escapes special keys with double quotes and supports lax modes; JSON_EXTRACT is deprecated and uses $['key'].
How do I get all values of a field inside a JSON array?
JSON_QUERY(payload, 'lax $.items.sku') returns them as one JSON array. For one row per element, use UNNEST(JSON_QUERY_ARRAY(payload, '$.items')) and read each element.
Related
Errors you may hit
More JSON functions
All BigQuery JSON functions · All functions · BigQuery error messages
