BigQuery · JSON function
BigQuery JSON_SET
JSON_SET(json, path, value[, path, value ...]) returns a copy of a JSON value with values inserted or replaced at the given JSONPaths, creating missing parents along the way. Pass create_if_missing => FALSE to update only keys that already exist.
JSON_SET edits a JSON value without rebuilding it. Path and value pairs are applied left to right, each on the result of the previous one. Existing values are overwritten; missing keys and their parent objects are created; an array index past the end pads the array with JSON nulls. Values are encoded with the same rules as TO_JSON, so you can set strings, numbers, timestamps, arrays and structs, and SQL NULL sets a JSON null.
The function doesn't change a table on its own. To modify stored data, use it in an UPDATE: SET payload = JSON_SET(payload, '$.status', 'refunded'). It accepts only the JSON type, so a STRING column needs PARSE_JSON first, and the paths must be constants.
Failures are quiet by design. If a path runs into a value of the wrong type, such as $.tags[0] when tags is a string, that pair is skipped and the others are applied. Only an invalid JSONPath raises an error; a NULL path skips its pair, and a NULL input returns NULL. The path $ replaces the whole value.
Syntax
JSON_SET(json_expr, json_path, value[, json_path, value ...][, create_if_missing => { TRUE | FALSE }])Arguments and return value
| Argument | Description |
|---|---|
json_expr | The JSON value to modify. STRING input is not accepted. |
json_path | A constant JSONPath where the value is inserted or replaced. $ replaces the whole value. |
value | Any SQL value that TO_JSON can encode. SQL NULL becomes a JSON null. |
create_if_missing | Optional named BOOL, default TRUE. FALSE replaces existing values only and skips paths that don't exist. |
Returns: JSON. NULL if json_expr is NULL.
Examples
Replace a field and add a nested one
The shipping object didn't exist, so JSON_SET creates it.
SELECT JSON_SET(
JSON '{"order_id": "A-100", "status": "pending"}',
'$.status', 'shipped',
'$.shipping.carrier', 'DHL'
) AS updated;| updated |
|---|
| {"order_id":"A-100","shipping":{"carrier":"DHL"},"status":"shipped"} |
Update only keys that exist
SELECT JSON_SET(
JSON '{"status": "pending"}',
'$.status', 'paid',
'$.paid_at', '2026-11-23',
create_if_missing => FALSE
) AS updated;| updated |
|---|
| {"status":"paid"} |
Type mismatches, array padding and nulls
tags is a string, so $.tags[0] is skipped without an error. Setting index 2 of a one-element array pads it with null.
SELECT
JSON_SET(JSON '{"tags": "sale"}', '$.tags[0]', 'new') AS type_mismatch_ignored,
JSON_SET(JSON '{"tags": ["sale"]}', '$.tags[2]', 'new') AS padded_with_nulls,
JSON_SET(JSON '{"coupon": "X1"}', '$.coupon', CAST(NULL AS STRING)) AS set_to_null;| type_mismatch_ignored | padded_with_nulls | set_to_null |
|---|---|---|
| {"tags":"sale"} | {"tags":["sale",null,"new"]} | {"coupon":null} |
Set arrays of structs and timestamps
SELECT JSON_SET(
JSON '{"order_id": "A-100"}',
'$.items', [STRUCT('KB-1' AS sku, 1 AS qty)],
'$.updated_at', TIMESTAMP '2026-11-23 10:00:00'
) AS updated;| updated |
|---|
| {"items":[{"qty":1,"sku":"KB-1"}],"order_id":"A-100","updated_at":"2026-11-23T10:00:00Z"} |
Common mistakes
- Passing a STRING column. JSON_SET needs JSON and fails with "No matching signature"; wrap the column in PARSE_JSON.
- Assuming a skipped pair raises an error. A wrong-type path is ignored silently, so check the output after bulk updates.
- Creating keys through typos. With the default create_if_missing => TRUE,
'$.stauts'adds a new key; pass FALSE when you only mean to update. - Expecting array insertion. Setting
$.items[0]replaces the first element;JSON_ARRAY_INSERTandJSON_ARRAY_APPENDinsert.
Cost and performance
- In an UPDATE, JSON_SET is billed like any DML statement: on-demand, by the bytes the statement scans. Filter on the partition column so only affected partitions are read.
FAQ
How do I update a field inside a JSON column in BigQuery?
UPDATE dataset.orders SET payload = JSON_SET(payload, '$.status', 'shipped') WHERE order_id = 'A-100'. Add create_if_missing => FALSE if the key must already exist.
How do I add a key to a JSON object?
JSON_SET(payload, '$.new_key', value). Missing parents are created as well, so '$.shipping.carrier' also creates shipping.
How do I remove a key instead?
Use JSON_REMOVE: JSON_REMOVE(payload, '$.coupon'). Setting the key to NULL with JSON_SET keeps it, with a JSON null value.
Related
Errors you may hit
More JSON functions
All BigQuery JSON functions · All functions · BigQuery error messages
