BigQuery · JSON function
BigQuery JSON_TYPE
JSON_TYPE(json) returns the type of a JSON value as a string: object, array, string, number, boolean or null. Use it to profile inconsistent payloads, tell a JSON null from a missing key, and pick the right conversion for each row.
JSON_TYPE looks at the outermost level of a JSON value and names its type. It accepts only the JSON type: for JSON text in a STRING, wrap it in PARSE_JSON first, otherwise the call fails with No matching signature for function JSON_TYPE.
It cleanly separates three cases that look alike elsewhere: a key holding a JSON null returns 'null', a missing key returns SQL NULL (because dot access returns SQL NULL for it), and a real value returns its type. That makes it useful for profiling: group a field by JSON_TYPE to see how many rows send numbers, strings or nothing at all.
Use the type to choose a converter, for example FLOAT64(x) for numbers and SAFE_CAST(STRING(x) AS FLOAT64) for numeric strings. The lax converters, such as LAX_FLOAT64, do this coercion for you and return NULL when a value can't be converted.
Syntax
JSON_TYPE(json_expr)Arguments and return value
| Argument | Description |
|---|---|
json_expr | A value of type JSON, such as a JSON column or a field reached with dot access. |
Returns: STRING: object, array, string, number, boolean or null. SQL NULL for SQL NULL input.
Examples
Every JSON type
SELECT
JSON_TYPE(JSON '{"id": 1}') AS object_type,
JSON_TYPE(JSON '[1, 2]') AS array_type,
JSON_TYPE(JSON '"A-100"') AS string_type,
JSON_TYPE(JSON '59.9') AS number_type,
JSON_TYPE(JSON 'true') AS boolean_type,
JSON_TYPE(JSON 'null') AS null_type;| object_type | array_type | string_type | number_type | boolean_type | null_type |
|---|---|---|---|---|---|
| object | array | string | number | boolean | null |
Profile a field across rows
The empty object has no amount key, so dot access and JSON_TYPE both return SQL NULL. The explicit JSON null is reported as 'null'.
WITH events AS (
SELECT JSON '{"amount": 10}' AS payload UNION ALL
SELECT JSON '{"amount": 12.5}' UNION ALL
SELECT JSON '{"amount": "10.50"}' UNION ALL
SELECT JSON '{"amount": null}' UNION ALL
SELECT JSON '{}'
)
SELECT JSON_TYPE(payload.amount) AS amount_type, COUNT(*) AS events
FROM events
GROUP BY amount_type
ORDER BY amount_type;| amount_type | events |
|---|---|
| NULL | 1 |
| null | 1 |
| number | 2 |
| string | 1 |
Convert each row according to its type
WITH events AS (
SELECT 1 AS id, JSON '{"amount": 10}' AS payload UNION ALL
SELECT 2, JSON '{"amount": "10.50"}' UNION ALL
SELECT 3, JSON '{"amount": "n/a"}'
)
SELECT
id,
CASE JSON_TYPE(payload.amount)
WHEN 'number' THEN FLOAT64(payload.amount)
WHEN 'string' THEN SAFE_CAST(STRING(payload.amount) AS FLOAT64)
END AS amount
FROM events
ORDER BY id;| id | amount |
|---|---|
| 1 | 10.0 |
| 2 | 10.5 |
| 3 | NULL |
Common mistakes
- Passing a STRING. JSON_TYPE accepts only JSON; use
JSON_TYPE(SAFE.PARSE_JSON(body))for text that may be invalid. - Expecting
integerorfloat. Every JSON number reportsnumber, so 10 and 12.5 look the same. - Treating
'null'and SQL NULL as the same thing.'null'means the key exists with a JSON null; SQL NULL means the key is missing or the input itself is NULL.
FAQ
How do I check if a JSON field is null or missing in BigQuery?
JSON_TYPE(payload.field) = 'null' finds explicit JSON nulls. payload.field IS NULL finds missing keys, because dot access returns SQL NULL for them and a JSON null is not SQL NULL.
How do I find JSON fields with mixed types?
Group by the type: SELECT JSON_TYPE(payload.amount) AS t, COUNT(*) FROM events GROUP BY t. More than one non-NULL group means producers disagree on the type.
Does JSON_TYPE work on STRING columns?
No. Parse the text first: JSON_TYPE(SAFE.PARSE_JSON(body)).
Related
Errors you may hit
More JSON functions
All BigQuery JSON functions · All functions · BigQuery error messages
