JSON_REPLACE

2024-06-28 05:30:31  Updated

Purpose

JSON_REPLACE() replaces the existing values in a JSON document and returns the results.

Syntax

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

Description

json_doc specifies the name of the JSON document, and path specifies the path. If any argument is NULL, the return value is NULL.

An error occurs in the following cases:

  • The JSON document specified for json_doc is invalid.

  • The path expression specified for path is invalid.

  • The specified path contains a * or ** wildcard.

Path-value pairs are evaluated from left to right. The document generated based on a path-value pair is used as the new value to evaluate the next pair.

The path-value pair of the existing path in the document overwrites the existing document value with the new value. The path-value pair of a path that does not exist in the document is ignored and 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

Contact Us