Purpose
Merges two or more JSON documents and returns the merged result.
Syntax
JSON_MERGE_PRESERVE(json_doc, json_doc[, json_doc] ...)
Description
The json_doc parameter specifies the JSON document to be merged.
If any parameter is NULL, the function returns NULL. If any parameter is not a valid JSON document, an error is returned.
The JSON_MERGE_PRESERVE() merge rule is as follows:
Merges adjacent arrays into one.
Adjacent objects are merged into one object.
Scalar values are automatically wrapped and merged into arrays.
Merges adjacent arrays and objects by automatically wrapping objects into arrays.
Examples
obclient> SELECT JSON_MERGE_PRESERVE('[1, 2, 3]', '[true, false]');
+---------------------------------------------------+
| JSON_MERGE_PRESERVE('[1, 2, 3]', '[true, false]') |
+---------------------------------------------------+
| [1, 2, 3, true, false] |
+---------------------------------------------------+
1 row in set
obclient> SELECT JSON_MERGE_PRESERVE('{"name": "apple"}', '{"id": 56}');
+--------------------------------------------------------+
| JSON_MERGE_PRESERVE('{"name": "apple"}', '{"id": 56}') |
+--------------------------------------------------------+
| {"id": 56, "name": "apple"} |
+--------------------------------------------------------+
1 row in set
obclient> SELECT JSON_MERGE_PRESERVE('1', 'true');
+----------------------------------+
| JSON_MERGE_PRESERVE('1', 'true') |
+----------------------------------+
| [1, true] |
+----------------------------------+
1 row in set
obclient> SELECT JSON_MERGE_PRESERVE('[1, 2, 3]', '{"id": 56}');
+------------------------------------------------+
| JSON_MERGE_PRESERVE('[1, 2, 3]', '{"id": 56}') |
+------------------------------------------------+
| [1, 2, 3, {"id": 56}] |
+------------------------------------------------+
1 row in set
obclient> SELECT JSON_MERGE_PRESERVE('{ "a": 1, "b": 2, "c": 5}', '{ "a": 3, "c": 4 }');
+-------------------------------------------------------------------------+
| JSON_MERGE_PRESERVE('{ "a": 1, "b": 2, "c": 5}', '{ "a": 3, "c": 4 }') |
+-------------------------------------------------------------------------+
| {"a": [1, 3], "b": 2, "c": [5, 4]} |
+-------------------------------------------------------------------------+
1 row in set
obclient> SELECT JSON_MERGE_PRESERVE('{ "a": 1, "b": 2 }','{ "a": 3, "c": 4 }', '{ "a": 5, "d": 6 }');
+--------------------------------------------------------------------------------------+
| JSON_MERGE_PRESERVE('{ "a": 1, "b": 2 }','{ "a": 3, "c": 4 }', '{ "a": 5, "d": 6 }') |
+--------------------------------------------------------------------------------------+
| {"a": [1, 3, 5], "b": 2, "c": 4, "d": 6} |
+--------------------------------------------------------------------------------------+
1 row in set