Purpose
JSON_MERGE_PRESERVE() merges two or more JSON documents and returns the merge results.
Syntax
JSON_MERGE_PRESERVE(json_doc, json_doc[, json_doc] ...)
Description
json_doc specifies the JSON documents to be merged.
If any argument is NULL, the return value is NULL. If any argument is not a valid JSON document, an error is returned.
Merge rules of the JSON_MERGE_PRESERVE() function are as follows:
Adjacent arrays are merged into one array.
Adjacent objects are merged into one object.
Scalar values are automatically wrapped and merged into arrays.
Objects are automatically wrapped into an array to merge adjacent arrays and objects.
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