Skip to main content

When to use this

Many APIs store complex data as JSON strings inside a single column — for example, an address field that contains {"city": "São Paulo", "state": "SP", "zip": "01310-100"}, or a metadata column with nested properties. To query individual values, you need to parse the JSON and extract the fields you care about.

Sample input

A customers table in the Raw layer where the address column is a JSON string:
customer_idnameaddress
1Alice Johnson{“city”: “São Paulo”, “state”: “SP”, “zip”: “01310-100”}
2Bob Smith{“city”: “New York”, “state”: “NY”, “zip”: “10001”}
3Carol Lee{“city”: “London”, “state”: “England”, “zip”: “EC1A 1BB”}
We want to extract city, state, and zip into their own columns.

Implementation

BigQuery uses JSON_EXTRACT_SCALAR (or the shorthand JSON_VALUE in Standard SQL) to pull values from a JSON string.
SELECT
  customer_id,
  name,
  JSON_VALUE(address, '$.city')  AS city,
  JSON_VALUE(address, '$.state') AS state,
  JSON_VALUE(address, '$.zip')   AS zip
FROM `raw.customers`
JSON_VALUE returns a STRING. To extract typed values, use JSON_EXTRACT with a CAST:
CAST(JSON_EXTRACT(data, '$.age') AS INT64) AS age
For arrays nested inside JSON, combine with JSON_EXTRACT_ARRAY and UNNEST.

Expected output

customer_idnamecitystatezip
1Alice JohnsonSão PauloSP01310-100
2Bob SmithNew YorkNY10001
3Carol LeeLondonEnglandEC1A 1BB

Tips and gotchas

JSON extraction returns NULL when a key doesn’t exist in the object. If some rows have different JSON shapes (e.g., some include country and others don’t), the missing keys will silently become NULL — combine this with the Handle NULL values recipe as needed.
If your JSON contains arrays (e.g., "tags": ["hot", "enterprise"]), you’ll need to combine JSON parsing with Unnest arrays. First extract the array with JSON functions, then unnest the result.