BigQuery · JSON function
BigQuery JSON_EXTRACT_SCALAR
JSON_EXTRACT_SCALAR(json, json_path) returns a scalar JSON value (string, number or boolean) as a SQL STRING without quotes, and NULL for objects, arrays and JSON null. It is deprecated: JSON_VALUE behaves the same and is the recommended replacement.
JSON_EXTRACT_SCALAR is the function most older BigQuery queries use to read a field from a JSON string. It strips the outer quotes and unescapes the value, so {"email": "ana@acme.io"} gives ana@acme.io, numbers give their text (42) and booleans give true or false. If the path points to an object, an array or a JSON null, the result is NULL. The path is optional and defaults to $.
Google now marks it deprecated and recommends JSON_VALUE. The results are identical; only key escaping differs. JSON_EXTRACT_SCALAR escapes special keys with single quotes in brackets ($['utm.source']), JSON_VALUE with double quotes ($."utm.source").
The result is always a STRING. Cast it before comparing or summing: as text, '9' > '10' is true. SAFE_CAST turns values like "n/a" into NULL instead of failing the query.
Syntax
JSON_EXTRACT_SCALAR(json_string_expr[, json_path])
JSON_EXTRACT_SCALAR(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, default $. Escape special keys as $['key.name']. |
Returns: STRING. NULL if the path is missing or points to an object, an array or JSON null.
Examples
No quotes, unlike JSON_EXTRACT
SELECT
JSON_EXTRACT('{"user": {"email": "ana@acme.io"}}', '$.user.email') AS with_extract,
JSON_EXTRACT_SCALAR('{"user": {"email": "ana@acme.io"}}', '$.user.email') AS with_extract_scalar;| with_extract | with_extract_scalar |
|---|---|
| "ana@acme.io" | ana@acme.io |
Objects and arrays return NULL
SELECT
JSON_EXTRACT_SCALAR('{"user": {"id": 42, "tags": ["vip"]}}', '$.user') AS object_value,
JSON_EXTRACT_SCALAR('{"user": {"id": 42, "tags": ["vip"]}}', '$.user.tags') AS array_value,
JSON_EXTRACT_SCALAR('{"user": {"id": 42, "tags": ["vip"]}}', '$.user.id') AS number_value;| object_value | array_value | number_value |
|---|---|---|
| NULL | NULL | 42 |
Cast amounts, including messy ones
Amounts arrive as strings in some events and numbers in others. SAFE_CAST handles both and turns n/a into NULL.
WITH events AS (
SELECT '{"order_id": "A-100", "amount": "59.90"}' AS payload UNION ALL
SELECT '{"order_id": "A-101", "amount": 120}' UNION ALL
SELECT '{"order_id": "A-102", "amount": "n/a"}'
)
SELECT
JSON_EXTRACT_SCALAR(payload, '$.order_id') AS order_id,
SAFE_CAST(JSON_EXTRACT_SCALAR(payload, '$.amount') AS NUMERIC) AS amount
FROM events
ORDER BY order_id;| order_id | amount |
|---|---|
| A-100 | 59.9 |
| A-101 | 120 |
| A-102 | NULL |
Migrating to JSON_VALUE
SELECT
JSON_EXTRACT_SCALAR('{"utm.source": "newsletter"}', "$['utm.source']") AS legacy,
JSON_VALUE('{"utm.source": "newsletter"}', '$."utm.source"') AS standard;| legacy | standard |
|---|---|
| newsletter | newsletter |
Common mistakes
- Comparing numbers as text.
JSON_EXTRACT_SCALAR(p, '$.qty') > '10'compares strings, so '9' is greater than '10'. Cast first. - Expecting an object or array back. The function returns NULL for them; use JSON_QUERY or JSON_QUERY_ARRAY.
- Using
$."key"escaping, which only JSON_VALUE and JSON_QUERY accept. - Reading NULL as "key missing". NULL also means the key holds a JSON null, an object or an array; JSON_TYPE on a JSON value tells them apart.
- Getting key case wrong. Paths are case-sensitive, so
$.useriddoesn't matchuserIdand silently returns NULL.
Cost and performance
- Every key you extract from a STRING payload is paid for with the whole column: on-demand billing counts all of its bytes. If the column is queried often, convert it once with
SAFE.PARSE_JSONinto a JSON column; later queries then bill only the paths they read.
FAQ
What is the difference between JSON_EXTRACT_SCALAR and JSON_VALUE?
They return the same results. JSON_VALUE is the current, recommended function and escapes special keys with double quotes ($."a.b"); JSON_EXTRACT_SCALAR is deprecated and uses $['a.b'].
How do I extract a number from a JSON string in BigQuery?
Extract it as text and cast: SAFE_CAST(JSON_VALUE(body, '$.amount') AS NUMERIC). On a JSON column, FLOAT64(payload.amount) or the lenient LAX_FLOAT64(payload.amount) convert without the text step.
Why does JSON_EXTRACT_SCALAR return NULL?
The path doesn't exist (keys are case-sensitive), the value is an object, an array or JSON null, or the STRING isn't valid JSON. Run JSON_QUERY with the same path on one row to see what is there.
Related
Errors you may hit
More JSON functions
All BigQuery JSON functions · All functions · BigQuery error messages
