BigQuery · JSON function
BigQuery PARSE_JSON
PARSE_JSON(json_string) converts JSON-formatted text into a value of the JSON type, validating it on the way. Use SAFE.PARSE_JSON to get NULL instead of an error for malformed rows, and wide_number_mode => 'round' to accept numbers too large to store exactly.
PARSE_JSON turns JSON text from a STRING column (webhook bodies, log payloads, exported events) into the JSON type. Once parsed, you can use dot access and subscripts (PARSE_JSON(body).user.id), JSON functions such as JSON_SET, and typed converters such as INT64(). CAST(body AS JSON) is not supported; PARSE_JSON is the conversion.
Parsing validates the text, and invalid JSON fails the query with "Invalid input: syntax error while parsing". SAFE.PARSE_JSON(body) returns NULL for those rows instead, which makes bad payloads easy to count and inspect. The text null parses to a JSON null, which is not SQL NULL.
The JSON type normalizes what it stores: whitespace is dropped, object keys are not kept in their original order, and when a key appears twice only the first is kept. Numbers must fit losslessly in INT64, UINT64 or FLOAT64. A larger number fails in the default wide_number_mode => 'exact'; 'round' stores the nearest FLOAT64 and loses digits, so keep long identifiers as strings in the source.
For constant JSON written in a query, a JSON literal (JSON '{"a": 1}') does the same job. To convert SQL values rather than text, use TO_JSON.
Syntax
PARSE_JSON(json_string_expr[, wide_number_mode => { 'exact' | 'round' }])Arguments and return value
| Argument | Description |
|---|---|
json_string_expr | A STRING containing JSON text. |
wide_number_mode | Optional named argument. 'exact' (default) fails on numbers that can't be stored without precision loss; 'round' rounds them to FLOAT64. |
Returns: JSON.
Examples
Parse a STRING column and read fields
WITH raw_events AS (
SELECT '{"event": "upgrade", "user": {"id": 42, "plan": "pro"}}' AS body
)
SELECT
PARSE_JSON(body).event AS event,
JSON_VALUE(PARSE_JSON(body), '$.user.plan') AS plan,
INT64(PARSE_JSON(body).user.id) AS user_id
FROM raw_events;| event | plan | user_id |
|---|---|---|
| "upgrade" | pro | 42 |
Skip malformed rows with SAFE.PARSE_JSON
Row 2 is truncated and becomes SQL NULL. Row 3 is valid JSON whose value is null.
WITH raw_events AS (
SELECT 1 AS id, '{"event": "signup"}' AS body UNION ALL
SELECT 2, '{"event": "signup"' UNION ALL
SELECT 3, 'null'
)
SELECT
id,
SAFE.PARSE_JSON(body) AS parsed,
SAFE.PARSE_JSON(body) IS NULL AS is_sql_null
FROM raw_events
ORDER BY id;| id | parsed | is_sql_null |
|---|---|---|
| 1 | {"event":"signup"} | false |
| 2 | NULL | true |
| 3 | null | false |
What the JSON type keeps and drops
Whitespace and the second a are gone, and the keys are no longer in input order.
SELECT PARSE_JSON('{ "b": 2, "a": 1, "a": 3 }') AS parsed;| parsed |
|---|
| {"a":1,"b":2} |
Numbers too large to store exactly
Without wide_number_mode => 'round' this fails with "cannot round-trip through string representation". The rounded value has lost its last digits.
SELECT PARSE_JSON('{"order_id": 12345678901234567890123}', wide_number_mode => 'round') AS parsed;| parsed |
|---|
| {"order_id":1.2345678901234568e+22} |
Common mistakes
- Writing
CAST(body AS JSON). It fails with "Invalid cast from STRING to JSON"; use PARSE_JSON. - Letting one bad payload fail a backfill. Use SAFE.PARSE_JSON and count the rows that came back NULL.
- Parsing very long numeric IDs. Numbers beyond the 64-bit integer range fail in exact mode and lose digits in round mode; quote such IDs in the source.
- Relying on key order or duplicate keys after parsing. The JSON type keeps the first duplicate and doesn't preserve member order; keep the original STRING if you need the raw text.
- Double-encoded payloads, where the object was stored as a JSON string, parse to a string (
JSON_TYPEreturnsstring). Parse the inner text a second time:PARSE_JSON(JSON_VALUE(PARSE_JSON(body))).
Cost and performance
- Parsing at query time repeats the work on every run and still bills the full STRING column. If the column is queried often, convert it once (
CREATE TABLE ... AS SELECT SAFE.PARSE_JSON(body) AS payload ...) so later queries read only the JSON paths they use.
FAQ
How do I convert a STRING to JSON in BigQuery?
PARSE_JSON(string_col), or SAFE.PARSE_JSON(string_col) to get NULL for invalid rows. CAST to JSON isn't supported.
What is the difference between PARSE_JSON and a JSON literal?
A literal (JSON '{"a": 1}') is a constant written in the query and is checked when the query is compiled. PARSE_JSON converts text from a column or expression at run time.
Why does PARSE_JSON fail with "cannot round-trip through string representation"?
A number in the text is too large to store exactly. Pass wide_number_mode => 'round' to accept a rounded FLOAT64, or keep the value as a string if every digit matters.
Related
More JSON functions
All BigQuery JSON functions · All functions · BigQuery error messages
