About the JSON_MERGE() Function in MySQL

In MySQL, JSON_MERGE() is a deprecated function that merges two or more JSON documents and returns the result.

It was deprecated in MySQL 8.0.3 and is subject to removal in a future release.

Fortunately, the JSON_MERGE_PRESERVE() function was added in MySQL 8.0.3 as a synonym for JSON_MERGE(), and it therefore does the same thing that JSON_MERGE() does/did.

Therefore, instead of using JSON_MERGE(), use JSON_MERGE_PRESERVE() instead.

Alternatively, you can use JSON_MERGE_PATCH(), which performs an RFC 7396 compliant merge of two or more JSON documents, without preserving members having duplicate keys.

Syntax

The syntax goes like this:

JSON_MERGE(json_doc, json_doc[, json_doc] ...)

Where json_doc are the JSON documents to merge.

Example

Here’s an example to demonstrate.

SELECT JSON_MERGE(
    '{"name":"Wag"}', 
    '{"type":"Dog"}'
    ) AS Result;

Result:

+--------------------------------+
| Result                         |
+--------------------------------+
| {"name": "Wag", "type": "Dog"} |
+--------------------------------+

We can see that the two documents have been merged into one.

As mentioned, JSON_MERGE() is deprecated and we should use JSON_MERGE_PRESERVE() instead. To do this, all we need to do is add _PRESERVE to the function name:

SELECT JSON_MERGE_PRESERVE(
    '{"name":"Wag"}', 
    '{"type":"Dog"}'
    ) AS Result;

Result:

+--------------------------------+
| Result                         |
+--------------------------------+
| {"name": "Wag", "type": "Dog"} |
+--------------------------------+

We can see that the two documents have been merged into one.

Merge More than Two Documents

Here’s an example that merges three documents:

SELECT JSON_MERGE(
    '{ "name" : "Wag" }', 
    '{ "type" : "Dog" }',
    '{ "score" : [ 9, 7, 8 ] }'
    ) AS Result;

Result:

+----------------------------------------------------+
| Result                                             |
+----------------------------------------------------+
| {"name": "Wag", "type": "Dog", "score": [9, 7, 8]} |
+----------------------------------------------------+

Arrays

Here’s an example of merging arrays:

SELECT JSON_MERGE(
    '[1,2,3]', 
    '[4,5,6]'
    ) AS Result;

Result:

+--------------------+
| Result             |
+--------------------+
| [1, 2, 3, 4, 5, 6] |
+--------------------+

We can do the same thing with JSON_MERGE_PRESERVE(), but not with JSON_MERGE_PATCH(). Attempting to merge arrays with JSON_MERGE_PATCH() results in only the second array being returned.

Null Argument

If any argument is NULL, the result is NULL:

SELECT 
    JSON_MERGE('{"a":1}', null) AS a,
    JSON_MERGE(null, '{"a":1}') AS b,
    JSON_MERGE(null, null) AS c;

Result:

+------+------+------+
| a    | b    | c    |
+------+------+------+
| NULL | NULL | NULL |
+------+------+------+

Incorrect Parameter Count

Calling the function without any arguments results in an error:

SELECT JSON_MERGE();

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_MERGE'

It’s the same when you provide just one argument:

SELECT JSON_MERGE('{"a":1}');

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_MERGE'