JSON_REPLACE
Description
The JSON_REPLACE function is used to replace data in JSON and return the result.
Syntax
JSON_REPLACE (<json_object>, <path>, <value>[, <path>, <value>, ...])
Parameters
<json_object>: JSON type expression, the target to be modified.<path>: String type expression, specifies the path where the value is to be replaced<value>: JSON type or other types supported byTO_JSON, the value to be replaced.
Return Value
- Nullable(JSON) Returns the modified JSON object
Usage Notes
- Note that path-value pairs are evaluated from left to right.
- If the value pointed to by
<path>does not exist in the JSON object, it will have no effect. <path>cannot contain wildcards, if it contains wildcards an error will be reported.- When
<json_object>or<path>is NULL, NULL will be returned. If<value>is NULL, a JSON null value will be inserted.
Examples
- Path-value pairs are evaluated from left to right
select json_replace('{"k": {"k2": "v2"}}', '$.k', json_parse('{"k2": 321, "k3": 456}'), '$.k.k2', 123);+-------------------------------------------------------------------------------------------------+
| json_replace('{"k": {"k2": "v2"}}', '$.k', json_parse('{"k2": 321, "k3": 456}'), '$.k.k2', 123) |
+-------------------------------------------------------------------------------------------------+
| {"k":{"k2":123,"k3":456}} |
+-------------------------------------------------------------------------------------------------+ - Value pointed to by
<path>does not exist in the JSON objectselect json_replace('{"k": 1}', "$.k2", 2);+-------------------------------------+
| json_replace('{"k": 1}', "$.k2", 2) |
+-------------------------------------+
| {"k":1} |
+-------------------------------------+ <path>cannot contain wildcardsselect json_replace('{"k": 1}', "$.*", 2);ERROR 1105 (HY000): errCode = 2, detailMessage = [INVALID_ARGUMENT] In this situation, path expressions may not contain the * and ** tokens or an array range, argument index: 1, row index: 0- NULL parameters
select json_replace(NULL, '$[1]', 123);+---------------------------------+
| json_replace(NULL, '$[1]', 123) |
+---------------------------------+
| NULL |
+---------------------------------+select json_replace('{"k": "v"}', NULL, 123);+---------------------------------------+
| json_replace('{"k": "v"}', NULL, 123) |
+---------------------------------------+
| NULL |
+---------------------------------------+select json_replace('{"k": "v"}', '$.k', NULL);+-----------------------------------------+
| json_replace('{"k": "v"}', '$.k', NULL) |
+-----------------------------------------+
| {"k":null} |
+-----------------------------------------+