I MariaDB, JSON_MERGE_PATCH() är en inbyggd funktion som slår samman två eller flera JSON-dokument och returnerar resultatet.
JSON_MERGE_PATCH() funktion är en RFC 7396-kompatibel ersättning för JSON_MERGE() funktion, som har fasats ut.
Syntax
Syntaxen ser ut så här:
JSON_MERGE_PATCH(json_doc, json_doc[, json_doc] ...)
Där json_doc är JSON-dokumenten som ska sammanfogas.
Exempel
Här är ett exempel att visa.
SELECT JSON_MERGE_PATCH('{"name":"Wag"}', '{"type":"Dog"}'); Resultat:
+------------------------------------------------------+
| JSON_MERGE_PATCH('{"name":"Wag"}', '{"type":"Dog"}') |
+------------------------------------------------------+
| {"name": "Wag", "type": "Dog"} |
+------------------------------------------------------+ Vi kan se att de två dokumenten har slagits samman till ett.
Här är ett exempel som slår samman tre dokument:
SELECT JSON_MERGE_PATCH(
'{ "name" : "Wag" }',
'{ "type" : "Dog" }',
'{ "score" : [ 9, 7, 8 ] }'
) AS Result; Resultat:
+----------------------------------------------------+
| Result |
+----------------------------------------------------+
| {"name": "Wag", "type": "Dog", "score": [9, 7, 8]} |
+----------------------------------------------------+ Arrayer
JSON_MERGE_PATCH() funktion slår inte samman arrayer:
SELECT JSON_MERGE_PATCH(
'[1,2,3]',
'[4,5,6]'
) AS Result; Resultat:
+-----------+ | Result | +-----------+ | [4, 5, 6] | +-----------+
Detta är i motsats till den (utfasade) JSON_MERGE() funktion och dess synonym JSON_MERGE_PRESERVE() , som båda slår samman arrayer.
Format för resultatet
Du kanske har märkt att JSON_MERGE_PATCH() lägger till mellanslag i det resulterande dokumentet. Om detta är ett problem kan du använda JSON_COMPACT() för att ta bort utrymmet.
Exempel:
SELECT
JSON_COMPACT(
JSON_MERGE_PATCH(
'{"name":"Wag"}',
'{"type":"Dog"}',
'{"score":[9,7,8]}'
)
) AS Result; Resultat:
+---------------------------------------------+
| Result |
+---------------------------------------------+
| {"name":"Wag","type":"Dog","score":[9,7,8]} |
+---------------------------------------------+
Men om du behöver gå åt andra hållet och få extra formatering, såsom indragna strukturer, prova JSON_DETAILED() funktion.
SELECT
JSON_DETAILED(
JSON_MERGE_PATCH(
'{ "name" : "Wag" }',
'{ "type" : "Dog" }',
'{ "score" : [ 9, 7, 8 ] }'
)
) AS Result; Resultat:
+---------------------------------------+
| Result |
+---------------------------------------+
| {
"name": "Wag",
"type": "Dog",
"score":
[
9,
7,
8
]
} |
+---------------------------------------+ Nollargument
Om något argument är NULL , resultatet är NULL :
SELECT
JSON_MERGE_PATCH('{"a":1}', null) AS a,
JSON_MERGE_PATCH(null, '{"a":1}') AS b,
JSON_MERGE_PATCH(null, null) AS c; Resultat:
+------+------+------+ | a | b | c | +------+------+------+ | NULL | NULL | NULL | +------+------+------+
Felaktig parameterräkning
Att anropa funktionen utan några argument resulterar i ett fel:
SELECT JSON_MERGE_PATCH(); Resultat:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_MERGE_PATCH'
Det är samma sak när du bara ger ett argument:
SELECT JSON_MERGE_PATCH('{"a":1}'); Resultat:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_MERGE_PATCH'