BigQuery · JSON function
BigQuery JSON_VALUE
JSON_VALUE(json[, json_path]) extracts a scalar (string, number or boolean) from JSON and returns it as a SQL STRING without quotes; objects, arrays and JSON null return NULL. It is the recommended replacement for JSON_EXTRACT_SCALAR and works on STRING and JSON input.
JSON_VALUE is the function to use when you want a field's value rather than its JSON: JSON_VALUE(body, '$.user.email') returns ana@acme.io. It accepts JSON text in a STRING or a native JSON value, and the path defaults to $, so JSON_VALUE(payload.user.email) works on a JSON column after dot access.
It replaces JSON_EXTRACT_SCALAR, which Google now marks deprecated, and returns the same results. The difference is the path syntax: a key that contains a dot or another JSONPath character is escaped with double quotes ('$."utm.source"'), and the legacy form $['utm.source'] fails with "Invalid token in JSONPath". The path must be a constant, and unlike JSON_QUERY it doesn't accept a lax mode.
The result is always STRING, so cast it before math or numeric comparisons. On a JSON column you can skip the text step with the typed converters INT64(), FLOAT64() and BOOL(), or the lenient LAX_INT64() family, which also accepts numbers stored as strings.
JSON_VALUE is also how you group, sort and filter by a JSON field. JSON values can't be compared or grouped, so WHERE payload.status = 'paid' fails with No matching signature for operator = and GROUP BY payload.country fails too, while JSON_VALUE(payload, '$.status') = 'paid' works.
Syntax
JSON_VALUE(json_string_expr[, json_path])
JSON_VALUE(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, including the result of dot access such as payload.user. |
json_path | Optional constant JSONPath, default $. Escape a key that contains a dot with double quotes: '$."utm.source"'. |
Returns: STRING without JSON quotes. NULL if the path is missing or points to an object, an array or JSON null.
Examples
Strings, numbers and booleans as text
SELECT
JSON_VALUE('{"user": {"id": 42, "email": "ana@acme.io", "verified": true}}', '$.user.email') AS email,
JSON_VALUE('{"user": {"id": 42, "email": "ana@acme.io", "verified": true}}', '$.user.id') AS user_id,
JSON_VALUE('{"user": {"id": 42, "email": "ana@acme.io", "verified": true}}', '$.user.verified') AS verified,
JSON_VALUE('{"user": {"id": 42, "email": "ana@acme.io", "verified": true}}', '$.user') AS user_object;| user_id | verified | user_object | |
|---|---|---|---|
| ana@acme.io | 42 | true | NULL |
Revenue by country from a JSON column
JSON fields can't be grouped directly, so extract them first. The amount is a number in some rows and a string in others; CAST handles both.
WITH events AS (
SELECT JSON '{"country": "DE", "amount": 40}' AS payload UNION ALL
SELECT JSON '{"country": "DE", "amount": "25.5"}' UNION ALL
SELECT JSON '{"country": "US", "amount": 10}'
)
SELECT
JSON_VALUE(payload, '$.country') AS country,
SUM(CAST(JSON_VALUE(payload, '$.amount') AS NUMERIC)) AS revenue
FROM events
GROUP BY country
ORDER BY country;| country | revenue |
|---|---|
| DE | 65.5 |
| US | 10 |
Keys that contain dots
$."utm.source" reads the key literally named utm.source; $.utm.source walks into the nested utm object.
SELECT
JSON_VALUE('{"utm.source": "newsletter", "utm": {"source": "ads"}}', '$."utm.source"') AS dotted_key,
JSON_VALUE('{"utm.source": "newsletter", "utm": {"source": "ads"}}', '$.utm.source') AS nested_path;| dotted_key | nested_path |
|---|---|
| newsletter | ads |
JSON null, missing keys, empty strings and case
SELECT
JSON_VALUE('{"coupon": null}', '$.coupon') IS NULL AS json_null,
JSON_VALUE('{}', '$.coupon') IS NULL AS missing_key,
JSON_VALUE('{"coupon": ""}', '$.coupon') = '' AS empty_string,
JSON_VALUE('{"Coupon": "X1"}', '$.coupon') IS NULL AS wrong_case;| json_null | missing_key | empty_string | wrong_case |
|---|---|---|---|
| true | true | true | true |
Common mistakes
- Doing math or numeric comparisons on the STRING result.
JSON_VALUE(p, '$.qty') > '10'compares text; cast to INT64 or NUMERIC first. - Using JSON_EXTRACT-style escaping
$['a.b'], which fails with "Invalid token in JSONPath". Write'$."a.b"'. - Expecting objects or arrays. They return NULL; use JSON_QUERY or JSON_VALUE_ARRAY.
- Comparing a JSON field directly with a string, as in
payload.status = 'paid'. JSON has no equality operator; wrap the field in JSON_VALUE. - Getting key case wrong. Paths are case-sensitive, and a wrong key returns NULL rather than an error.
Cost and performance
- With JSON stored in a STRING column, every extraction reads the whole string, so on-demand billing counts the full column however few keys you pull. A native JSON column is stored as separate virtual columns per path, and BigQuery bills only the paths a query reads; the dry-run estimate still shows the full column size as an upper bound.
FAQ
What is the difference between JSON_VALUE and JSON_EXTRACT_SCALAR?
They return the same values. JSON_VALUE is the recommended function and escapes special keys with double quotes ('$."a.b"'); JSON_EXTRACT_SCALAR is deprecated and uses $['a.b'].
What is the difference between JSON_VALUE and JSON_QUERY?
JSON_VALUE returns a scalar as a plain STRING and NULL for objects and arrays. JSON_QUERY returns any JSON value, objects and arrays included, and keeps the quotes on strings.
How do I get an integer from JSON in BigQuery?
CAST(JSON_VALUE(body, '$.qty') AS INT64), or SAFE_CAST if some values aren't numbers. On a JSON column, INT64(payload.qty) converts directly and LAX_INT64(payload.qty) also accepts numeric strings.
How do I filter rows by a JSON field?
WHERE JSON_VALUE(payload, '$.status') = 'paid'. Comparing the JSON field itself fails, because JSON values have no equality operator.
Related
Errors you may hit
More JSON functions
All BigQuery JSON functions · All functions · BigQuery error messages
