SQL JSON_REPLACE function
JSON_REPLACE(config, '$.region', 'us') updates keys that exist and ignores the rest. The safe half of JSON_SET for updates.
- Catégorie
- Strings
- Returns
JSON text- In this category
- 148 of 172
Syntaxe
JSON_REPLACE(json_doc, path, value [, path, value ...])
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Not built in | JSON_MODIFY with a 'strict $...' path errors instead of creating |
| PostgreSQL | Not built in | jsonb_set(doc, path, val, false) never creates a missing key |
| MySQL | Oui | Supported under this name. |
| SQLite | Oui | JSON1 functions |
| Oracle | Not built in | No built-in equivalent. |
Exemples
MySQL
UPDATE settings SET config = JSON_REPLACE(config, '$.region', 'us');
SQLite
UPDATE settings SET config = json_replace(config, '$.tier', 'pro');
Bon à savoir
- A path that does not exist is skipped without warning: a typo in '$.regon' changes nothing at all. Verify with JSON_CONTAINS_PATH or a SELECT before the UPDATE.
- JSON_SET = insert + replace combined; JSON_REPLACE matters only when new keys must not appear (schema-preserved documents).
- PostgreSQL's jsonb_set creates missing keys by default; pass false as the fourth argument (create_if_missing) to get replace-only behaviour.
Questions about JSON_REPLACE
Why did my JSON_REPLACE change nothing?
The path did not exist, and JSON_REPLACE never creates one. Run SELECT JSON_EXTRACT(config, '$.region') first to check the spelling.
Which function guarantees no new keys?
JSON_REPLACE. JSON_SET would create the key if missing, which is exactly the difference to test for in schema-sensitive documents.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
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.