Request a demo

BigQuery · JSON function

BigQuery JSON_KEYS

JSON_KEYS(json[, max_depth][, mode => ...]) returns the unique keys of a JSON value as a sorted ARRAY<STRING>, with nested keys as dotted paths such as user.id. Use it to discover and compare the schemas of semi-structured payloads.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

JSON_KEYS walks a JSON object and lists every key it finds, deduplicated and sorted alphabetically. Nested keys come back as paths joined with dots (user.plan), and max_depth limits how deep it goes: 1 returns only top-level keys. A key that itself contains a dot is wrapped in double quotes ("utm.source") so it can't be confused with a nested path.

By default (mode => 'strict') keys inside arrays are skipped, so an items array of objects contributes only items. mode => 'lax' also returns keys of objects inside arrays (items.sku), and 'lax recursive' follows arrays nested in arrays as well. Array indexes never appear in the output.

The input must be the JSON type. A scalar, or a top-level array in strict mode, returns an empty array, and max_depth must be positive. Keys are case-sensitive, which is what makes JSON_KEYS good at spotting producers that send userId and user_id for the same field.

Syntax

JSON_KEYS(json_expr[, max_depth][, mode => { 'strict' | 'lax' | 'lax recursive' }])

Arguments and return value

ArgumentDescription
json_exprA value of type JSON.
max_depthOptional positive INT64. 1 returns top-level keys only. NULL means no limit; 0 or less is an error.
modeOptional named STRING: 'strict' (default) ignores keys inside arrays, 'lax' includes keys of objects in arrays, 'lax recursive' includes all keys.

Returns: ARRAY<STRING> of unique keys in alphabetical order. NULL if json_expr or mode is NULL.

Examples

All keys, nested ones as paths

SELECT JSON_KEYS(JSON '{"event": "purchase", "user": {"id": 42, "plan": "pro"}}') AS all_keys;
Result
all_keys
[event, user, user.id, user.plan]

Top-level keys only

SELECT JSON_KEYS(JSON '{"event": "purchase", "user": {"id": 42, "plan": "pro"}}', 1) AS top_level_keys;
Result
top_level_keys
[event, user]

Keys inside arrays need lax mode

SELECT
  JSON_KEYS(JSON '{"order_id": "A-1", "items": [{"sku": "KB-1"}, {"qty": 2}]}') AS strict_mode,
  JSON_KEYS(JSON '{"order_id": "A-1", "items": [{"sku": "KB-1"}, {"qty": 2}]}', mode => 'lax') AS lax_mode;
Result
strict_modelax_mode
[items, order_id][items, items.qty, items.sku, order_id]

Spot schema drift across payloads

One producer sends userId instead of user_id.

WITH events AS (
  SELECT JSON '{"event": "signup", "user_id": 1}' AS payload UNION ALL
  SELECT JSON '{"event": "purchase", "user_id": 2, "amount": 30}' UNION ALL
  SELECT JSON '{"event": "purchase", "userId": 3, "amount": 12}'
)
SELECT key, COUNT(*) AS events_with_key
FROM events
CROSS JOIN UNNEST(JSON_KEYS(payload, 1)) AS key
GROUP BY key
ORDER BY key;
Result
keyevents_with_key
amount2
event3
userId1
user_id2

Common mistakes

  • Expecting keys inside arrays in the default mode. Strict mode skips them; pass mode => 'lax'.
  • Passing a STRING. JSON_KEYS accepts only JSON; parse the text with PARSE_JSON first.
  • Reading a quoted entry as a nested path. "utm.source" in quotes is one key with a dot in its name; utm.medium without quotes is a nested key.
  • Passing 0 as max_depth to mean no limit. It raises "max_depth must be positive"; leave it out or pass NULL.

Cost and performance

  • JSON_KEYS has to read every path of the JSON value, so running it over a whole table bills the full JSON column. Profile one recent partition or a TABLESAMPLE before scanning all history.

FAQ

How do I get all keys of a JSON object in BigQuery?

JSON_KEYS(json_col) returns them as a sorted array, with nested keys as dotted paths. Use JSON_KEYS(json_col, 1) for top-level keys only.

How do I list the distinct keys across all rows?

Unnest and aggregate: SELECT key, COUNT(*) FROM t CROSS JOIN UNNEST(JSON_KEYS(t.payload, 1)) AS key GROUP BY key.

Why doesn't JSON_KEYS return the keys inside my array?

The default strict mode ignores keys within arrays. Pass mode => 'lax', or 'lax recursive' for arrays nested in arrays.

More JSON functions

All BigQuery JSON functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.