BigQuery · JSON function
BigQuery JSON_EXTRACT
JSON_EXTRACT(json, json_path) returns the JSON at a path (an object, an array or a quoted string) as JSON-formatted text, or as JSON for JSON input. Google marks it deprecated: JSON_QUERY returns the same values and is the function to use in new code.
JSON_EXTRACT reads a value out of JSON with a JSONPath such as '$.order.id' or '$.items[0]'. It accepts JSON text stored in a STRING or a native JSON value. For STRING input it returns a JSON-formatted STRING: objects come back as compact JSON text and string values keep their double quotes ("A-100"). For JSON input it returns JSON.
The GoogleSQL reference now lists JSON_EXTRACT as deprecated in favor of JSON_QUERY. Existing queries keep working, and both return the same results. The visible difference is how a key with special characters is escaped in the path: JSON_EXTRACT uses single quotes in brackets ($['utm.source']), JSON_QUERY uses double quotes ($."utm.source"). Each rejects the other's form with "Invalid token in JSONPath".
To get a value without quotes, use JSON_VALUE, or the deprecated JSON_EXTRACT_SCALAR. A JSON null comes back as SQL NULL for STRING input but as a JSON null value for JSON input. STRING input isn't validated: BigQuery parses only as far as it needs, so a truncated document can still return a value and text that isn't JSON returns NULL instead of an error.
The path must be a constant, either a literal or a query parameter. A path built from a column fails with "must be a constant expression". For dynamic keys, convert to JSON and use the subscript operator: PARSE_JSON(body)[key_column].
Syntax
JSON_EXTRACT(json_string_expr, json_path)
JSON_EXTRACT(json_expr, json_path)Arguments and return value
| Argument | Description |
|---|---|
json_string_expr | JSON-formatted text in a STRING, for example a raw webhook body. |
json_expr | A value of type JSON. |
json_path | A constant JSONPath starting with $, such as '$.order.id' or '$.items[0].sku'. Escape a key that contains a dot as $['utm.source']. |
Returns: A JSON-formatted STRING for STRING input, JSON for JSON input. NULL when the path doesn't match.
Examples
Objects, strings and numbers
The order id keeps its JSON quotes; the number doesn't have any.
SELECT
JSON_EXTRACT('{"order": {"id": "A-100", "total": 59.9}}', '$.order') AS order_json,
JSON_EXTRACT('{"order": {"id": "A-100", "total": 59.9}}', '$.order.id') AS order_id_json,
JSON_EXTRACT('{"order": {"id": "A-100", "total": 59.9}}', '$.order.total') AS total_json;| order_json | order_id_json | total_json |
|---|---|---|
| {"id":"A-100","total":59.9} | "A-100" | 59.9 |
Array elements by index
WITH webhooks AS (
SELECT '{"order": {"id": "A-100"}, "items": [{"sku": "KB-1"}, {"sku": "MS-2"}]}' AS body
)
SELECT
JSON_EXTRACT(body, '$.items[0]') AS first_item,
JSON_EXTRACT(body, '$.items[1].sku') AS second_sku,
JSON_EXTRACT(body, '$.items[5]') AS no_such_item
FROM webhooks;| first_item | second_sku | no_such_item |
|---|---|---|
| {"sku":"KB-1"} | "MS-2" | NULL |
Keys with dots: legacy and standard escaping
The key is literally named utm.source. JSON_EXTRACT escapes it with single quotes in brackets, JSON_QUERY with double quotes.
SELECT
JSON_EXTRACT('{"utm.source": "newsletter"}', "$['utm.source']") AS legacy_path,
JSON_QUERY('{"utm.source": "newsletter"}', '$."utm.source"') AS standard_path;| legacy_path | standard_path |
|---|---|
| "newsletter" | "newsletter" |
JSON null and missing keys
With STRING input a JSON null becomes SQL NULL. With JSON input it stays a JSON null, which is not SQL NULL. A missing key is SQL NULL either way.
SELECT
JSON_EXTRACT('{"coupon": null}', '$.coupon') IS NULL AS string_input_is_null,
JSON_EXTRACT(JSON '{"coupon": null}', '$.coupon') IS NULL AS json_input_is_null,
JSON_EXTRACT(JSON '{"coupon": null}', '$.discount') IS NULL AS missing_key_is_null;| string_input_is_null | json_input_is_null | missing_key_is_null |
|---|---|---|
| true | false | true |
Common mistakes
- Comparing the result with plain text.
JSON_EXTRACT(body, '$.status') = 'paid'is never true, because the result is"paid"with quotes; use JSON_VALUE. - Using JSON_QUERY's double-quote escaping (
$."utm.source") here. JSON_EXTRACT fails with "Invalid token in JSONPath"; write$['utm.source']. - Building the path from a column or a variable expression. JSONPath arguments must be constant.
- Doing math on the output. A number comes back as STRING text; convert it with
CAST(JSON_VALUE(body, '$.total') AS NUMERIC). - Assuming malformed JSON fails the query. With STRING input it returns NULL, or even a value, so bad payloads go unnoticed; PARSE_JSON validates them.
Cost and performance
- Extracting from a STRING column reads every byte of the string, so on-demand billing counts the whole column even when you need one key. In a JSON column, BigQuery stores paths as separate virtual columns and bills only the paths a query reads.
FAQ
Is JSON_EXTRACT deprecated in BigQuery?
Yes. The GoogleSQL reference marks it deprecated and points to JSON_QUERY. It still runs; to migrate, rename the function and change $['key'] escapes to $."key".
What is the difference between JSON_EXTRACT and JSON_EXTRACT_SCALAR?
JSON_EXTRACT returns JSON text: strings keep their quotes and objects and arrays come back whole. JSON_EXTRACT_SCALAR returns only scalars, as a STRING without quotes, and NULL for objects and arrays.
Why does JSON_EXTRACT return the value with quotes?
It returns JSON, and a JSON string is quoted. Use JSON_VALUE to get A-100 instead of "A-100".
Related
Errors you may hit
More JSON functions
All BigQuery JSON functions · All functions · BigQuery error messages
