In MariaDB, JSON_ARRAY_APPEND() is een ingebouwde functie die waarden toevoegt aan het einde van de opgegeven array/s binnen een JSON-document en het resultaat retourneert.
Syntaxis
De syntaxis gaat als volgt:
JSON_ARRAY_APPEND(json_doc, path, value[, path, value] ...)
Waar json_doc is het JSON-document, path is het pad naar waar u de waarde(n) wilt toevoegen, en value is de waarde die moet worden toegevoegd.
Voorbeeld
Hier is een voorbeeld om de functie te demonstreren.
SET @json_doc = '[0, 1, 2, 3]';
SELECT JSON_ARRAY_APPEND(@json_doc, '$', 4); Resultaat:
+--------------------------------------+ | JSON_ARRAY_APPEND(@json_doc, '$', 4) | +--------------------------------------+ | [0, 1, 2, 3, 4] | +--------------------------------------+
In dit geval is de waarde 4 werd toegevoegd aan het einde van de array.
Meerdere waarden toevoegen
U kunt meerdere waarden binnen één aanroep toevoegen aan JSON_ARRAY_APPEND() .
Voorbeeld:
SET @json_doc = '[0, 1, 2, 3]';
SELECT JSON_ARRAY_APPEND(@json_doc, '$', 4, '$', 5); Resultaat:
+----------------------------------------------+ | JSON_ARRAY_APPEND(@json_doc, '$', 4, '$', 5) | +----------------------------------------------+ | [0, 1, 2, 3, 4, 5] | +----------------------------------------------+
Meerdere arrays
U kunt waarden toevoegen aan meer dan één array binnen dezelfde aanroep naar JSON_ARRAY_APPEND() .
Voorbeeld:
SET @json_doc = '{"a": [0, 1], "b": [2, 3]}';
SELECT JSON_ARRAY_APPEND(@json_doc, '$.a', 4, '$.b', 5); Resultaat:
+--------------------------------------------------+
| JSON_ARRAY_APPEND(@json_doc, '$.a', 4, '$.b', 5) |
+--------------------------------------------------+
| {"a": [0, 1, 4], "b": [2, 3, 5]} |
+--------------------------------------------------+ Geneste arrays
Hier is een voorbeeld van het toevoegen van een waarde aan een geneste array:
SET @json_doc = '[0, 1, [2, 3]]';
SELECT JSON_ARRAY_APPEND(@json_doc, '$[2]', 4); Resultaat:
+-----------------------------------------+ | JSON_ARRAY_APPEND(@json_doc, '$[2]', 4) | +-----------------------------------------+ | [0, 1, [2, 3, 4]] | +-----------------------------------------+
En in het volgende voorbeeld bevat het originele JSON-document geen geneste array, maar JSON_ARRAY_APPEND() maakt een geneste array op basis van ons pad:
SET @json_doc = '[0, 1, 2, 3]';
SELECT JSON_ARRAY_APPEND(@json_doc, '$[3]', 4); Resultaat:
+-----------------------------------------+ | JSON_ARRAY_APPEND(@json_doc, '$[3]', 4) | +-----------------------------------------+ | [0, 1, 2, [3, 4]] | +-----------------------------------------+
Groter JSON-document
Hier is een voorbeeld met een iets groter JSON-document.
Ik gebruik ook JSON_DETAILED() om het resultaat mooier te maken:
SET @json_doc = '{
"pet": {
"name": "Fluffy",
"diet": ["Fish", "Chicken"]
}
}';
SELECT JSON_DETAILED(
JSON_ARRAY_APPEND(
@json_doc,
'$.pet.diet',
'Water')
); Resultaat:
{
"pet":
{
"name": "Fluffy",
"diet":
[
"Fish",
"Chicken",
"Water"
]
}
} En hier is er een die een geneste array maakt:
SET @json_doc = '{
"pet": {
"name": "Scratch",
"diet": ["Beef", "Water"]
}
}';
SELECT JSON_DETAILED(
JSON_ARRAY_APPEND(
@json_doc,
'$.pet.diet[1]',
'Beer')
); Resultaat:
{
"pet":
{
"name": "Scratch",
"diet":
[
"Beef",
[
"Water",
"Beer"
]
]
}
} Nullargumenten
Als het eerste argument NULL . is , het resultaat is NULL :
SELECT JSON_ARRAY_APPEND(null, '$', 4); Resultaat:
+---------------------------------+ | JSON_ARRAY_APPEND(null, '$', 4) | +---------------------------------+ | NULL | +---------------------------------+
Hetzelfde geldt voor het path argument:
SET @json_doc = '[0, 1, 2, 3]';
SELECT JSON_ARRAY_APPEND(@json_doc, null, 4); Resultaat:
+---------------------------------------+ | JSON_ARRAY_APPEND(@json_doc, null, 4) | +---------------------------------------+ | NULL | +---------------------------------------+
Als de value argument is NULL , dan NULL wordt aan de array toegevoegd:
SET @json_doc = '[0, 1, 2, 3]';
SELECT JSON_ARRAY_APPEND(@json_doc, '$', null); Resultaat:
+-----------------------------------------+ | JSON_ARRAY_APPEND(@json_doc, '$', null) | +-----------------------------------------+ | [0, 1, 2, 3, null] | +-----------------------------------------+
U kunt ook JSON_ARRAY_INSERT() . gebruiken om waarden in een array in te voegen.