Purpose
This function replaces existing values in a JSON document and returns the result.
Syntax
JSON_REPLACE(json_doc, path, val[, path, val] ...)
Considerations
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_docparameter is not a valid JSON document.The
pathparameter is not a valid path expression.The
pathparameter contains the * or ** wildcard.
Path-value pairs are evaluated from left to right. The document generated by the path-value pairs 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
obclient> SET @jn = '{ "a": 1, "b": [2, 3, 4]}';
Query OK, 0 rows affected
obclient> 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
