SQL JSON_MERGEPATCH function
JSON_MERGEPATCH(doc, '{"region": "us", "old": null}') sets region and deletes old in one call. Nested objects in the patch merge recursively.
- Kategorie
- Strings
- Returns
JSON text- In this category
- 160 of 172
Syntax
JSON_MERGEPATCH(target, patch) -- Oracle
JSON_MERGE_PATCH(target, patch) -- MySQL
json_patch(target, patch) -- SQLite
Each block is a separate form. The support table and the examples show which engine takes which.
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Not built in | no merge patch; JSON_MODIFY per path |
| PostgreSQL | Different name or syntax | jsonb || is a shallow merge, not RFC 7396 |
| MySQL | Ja | spelled JSON_MERGE_PATCH (8.0.3+) |
| SQLite | Ja | Supported under this name. |
| Oracle | Ja | JSON_MERGEPATCH, 19c+ |
Beispiele
MySQL
UPDATE settings
SET config = JSON_MERGE_PATCH(config,
'{"region": "us", "legacy": null}');
SQLite
SELECT json_patch(config, '{"tier": "pro"}')
FROM settings;
PostgreSQL
UPDATE settings
SET config = config || '{"tier": "pro"}'::jsonb;
Gut zu wissen
- A null value in the patch deletes the target member under RFC 7396: {"a": null} removes a; it does not store JSON null. The single most surprising rule of the format.
- PostgreSQL's || is a shallow merge, not merge-patch: nested objects are replaced whole instead of deep-merged, and nulls do not delete anything.
- MySQL spells it with an extra underscore, JSON_MERGE_PATCH; JSON_MERGEPATCH is Oracle's name. MySQL's deprecated JSON_MERGE is JSON_MERGE_PRESERVE, which combines duplicate keys into arrays instead of replacing them.
- Arrays are replaced, not merged: patching {"tags": ["a"]} with {"tags": ["b"]} gives ["b"].
Questions about JSON_MERGEPATCH
How do I deep-merge in PostgreSQL?
Not with ||, which replaces nested objects whole. Use jsonb_set for each nested path you change, write a recursive SQL function over jsonb_each, or merge in the application.
Why did my member disappear instead of becoming null?
RFC 7396 defines null in a patch as delete. To store an actual JSON null, use a path function instead: JSON_SET(doc, '$.a', CAST('null' AS JSON)) on MySQL, json_set(doc, '$.a', json('null')) on SQLite.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.