Skip to main content
Version: V1.0.0

JSON_REPLACE

Description

This function replaces existing values in a JSON document and returns the result.

Syntax

JSON_REPLACE(json_doc, path, val[, path, val] ...)

Description

The json_doc parameter specifies the JSON document, and path is the path parameter. If any parameter is NULL, the function returns NULL.

The following situations will result in an error:

  • The json_doc parameter is not a valid JSON document.

  • The path parameter is not a valid path expression.

  • The path parameter contains the * or ** wildcard.

Path-value pairs are evaluated from left to right. The document generated by each path-value pair is used as the new value for the next pair.

Existing path-value pairs in the document will be overwritten by the new values. Path-value pairs that do not exist in the document will be ignored and considered invalid.

Examples

SET @jn = '{ "a": 1, "b": [2, 3, 4]}';
Query OK, 0 rows affected (0.000 sec)

SELECT JSON_REPLACE(@jn, '$.a', 10, '$.c', '[true, false]');
+------------------------------------------------------+
| JSON_REPLACE(@jn, '$.a', 10, '$.c', '[true, false]') |
+------------------------------------------------------+
| {"a": 10, "b": [2, 3, 4]} |
+------------------------------------------------------+
1 row in set (0.001 sec)