// strings reference

SQL JSON_REMOVE function

JSON_REMOVE(config, '$.legacy') drops a member or array element. Missing paths are not errors; the document comes back unchanged.

类别
Strings
Returns
JSON text
In this category
149 of 172

语法

JSON_REMOVE(json_doc, path [, path ...])

引擎支持

引擎Support备注
SQL Server Not built in JSON_MODIFY(doc, '$.key', NULL) deletes in lax mode
PostgreSQL Different name or syntax jsonb - 'key' for top-level keys, #- '{a,b}' for paths
MySQL 是 Supported under this name.
SQLite 是 Supported under this name.
Oracle Not built in JSON_TRANSFORM on 21c+ has remove

示例

MySQL

UPDATE settings
SET config = JSON_REMOVE(config, '$.legacy');

SQLite

SELECT json_remove(payload, '$.items[0]') AS trimmed
FROM events;

PostgreSQL

UPDATE settings
SET config = config - 'legacy';

值得了解

  • Removing an array element shifts the indexes: delete '$.items[0]' and the old items[1] becomes items[0]. Remove from the end when deleting several, or collect the survivors instead.
  • A missing path returns the document unchanged: no error, no warning, so typos vanish silently.
  • SQL Server deletes by setting NULL: JSON_MODIFY(doc, '$.legacy', NULL) removes the key in lax mode. Storing an actual JSON null needs 'strict $.legacy'.

Questions about JSON_REMOVE

How do I delete from an array by value?

PostgreSQL: tags - 'old' removes matching string elements from a jsonb array. MySQL: JSON_REMOVE(doc, JSON_UNQUOTE(JSON_SEARCH(doc, 'one', 'old'))) removes the first match. Otherwise rebuild the array with json_each or JSON_TABLE.

Does removing the last element empty the array?

Yes, and removing all elements of a container leaves the empty container, not SQL NULL. JSON_REMOVE only ever edits the document it is given.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: PostgreSQL, MySQL, SQLite.

// fewer lookups

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.