Changing JSON in place in SQL
Editing a document without parsing it in your application first.
UPDATE webhooks
SET payload = JSON_SET(payload, '$.processed', 1)
WHERE payload ->> '$.kind' = 'order.paid';
SELECT
JSON_INSERT(payload, '$.retries', 0) AS with_retries,
JSON_REPLACE(payload, '$.kind', 'order.settled') AS renamed,
JSON_REMOVE(payload, '$.tags') AS without_tags,
JSON_PATCH(payload, '{"kind":"order.void","note":"merged"}') AS patched
FROM webhooks;
How it works
json_setadds or overwrites;json_insertrefuses to overwrite.json_replaceonly touches paths that already exist.json_removedeletes a path;json_patchmerges a whole object in.
Keywords and builtins used here
ASFROMSELECTSETUPDATEWHERE
The run, in numbers
- Lines
- 10
- Characters to type
- 366
- Tokens
- 65
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 41 seconds.
Step 3 of 5 in Writing JSON, step 6 of 14 in JSON & search.