In MariaDB, JSON_ARRAY_APPEND() è una funzione incorporata che aggiunge valori alla fine degli array specificati all'interno di un documento JSON e restituisce il risultato.
Sintassi
La sintassi è questa:
JSON_ARRAY_APPEND(json_doc, path, value[, path, value] ...)
Dove json_doc è il documento JSON, path è il percorso in cui vuoi aggiungere il/i valore/i e il value è il valore da aggiungere.
Esempio
Ecco un esempio per dimostrare la funzione.
SET @json_doc = '[0, 1, 2, 3]';
SELECT JSON_ARRAY_APPEND(@json_doc, '$', 4); Risultato:
+--------------------------------------+ | JSON_ARRAY_APPEND(@json_doc, '$', 4) | +--------------------------------------+ | [0, 1, 2, 3, 4] | +--------------------------------------+
In questo caso, il valore 4 è stato aggiunto alla fine dell'array.
Aggiungi più valori
Puoi aggiungere più valori all'interno di una singola chiamata a JSON_ARRAY_APPEND() .
Esempio:
SET @json_doc = '[0, 1, 2, 3]';
SELECT JSON_ARRAY_APPEND(@json_doc, '$', 4, '$', 5); Risultato:
+----------------------------------------------+ | JSON_ARRAY_APPEND(@json_doc, '$', 4, '$', 5) | +----------------------------------------------+ | [0, 1, 2, 3, 4, 5] | +----------------------------------------------+
Matrici multiple
Puoi aggiungere valori a più di un array all'interno della stessa chiamata a JSON_ARRAY_APPEND() .
Esempio:
SET @json_doc = '{"a": [0, 1], "b": [2, 3]}';
SELECT JSON_ARRAY_APPEND(@json_doc, '$.a', 4, '$.b', 5); Risultato:
+--------------------------------------------------+
| JSON_ARRAY_APPEND(@json_doc, '$.a', 4, '$.b', 5) |
+--------------------------------------------------+
| {"a": [0, 1, 4], "b": [2, 3, 5]} |
+--------------------------------------------------+ Matrici nidificate
Ecco un esempio di aggiunta di un valore a una matrice nidificata:
SET @json_doc = '[0, 1, [2, 3]]';
SELECT JSON_ARRAY_APPEND(@json_doc, '$[2]', 4); Risultato:
+-----------------------------------------+ | JSON_ARRAY_APPEND(@json_doc, '$[2]', 4) | +-----------------------------------------+ | [0, 1, [2, 3, 4]] | +-----------------------------------------+
E nell'esempio seguente, il documento JSON originale non contiene un array nidificato, ma JSON_ARRAY_APPEND() crea un array annidato basato sul nostro percorso:
SET @json_doc = '[0, 1, 2, 3]';
SELECT JSON_ARRAY_APPEND(@json_doc, '$[3]', 4); Risultato:
+-----------------------------------------+ | JSON_ARRAY_APPEND(@json_doc, '$[3]', 4) | +-----------------------------------------+ | [0, 1, 2, [3, 4]] | +-----------------------------------------+
Documento JSON più grande
Ecco un esempio con un documento JSON leggermente più grande.
Uso anche JSON_DETAILED() per abbellire il risultato:
SET @json_doc = '{
"pet": {
"name": "Fluffy",
"diet": ["Fish", "Chicken"]
}
}';
SELECT JSON_DETAILED(
JSON_ARRAY_APPEND(
@json_doc,
'$.pet.diet',
'Water')
); Risultato:
{
"pet":
{
"name": "Fluffy",
"diet":
[
"Fish",
"Chicken",
"Water"
]
}
} Ed eccone uno che crea un array nidificato:
SET @json_doc = '{
"pet": {
"name": "Scratch",
"diet": ["Beef", "Water"]
}
}';
SELECT JSON_DETAILED(
JSON_ARRAY_APPEND(
@json_doc,
'$.pet.diet[1]',
'Beer')
); Risultato:
{
"pet":
{
"name": "Scratch",
"diet":
[
"Beef",
[
"Water",
"Beer"
]
]
}
} Argomenti nulli
Se il primo argomento è NULL , il risultato è NULL :
SELECT JSON_ARRAY_APPEND(null, '$', 4); Risultato:
+---------------------------------+ | JSON_ARRAY_APPEND(null, '$', 4) | +---------------------------------+ | NULL | +---------------------------------+
Lo stesso vale per il path argomento:
SET @json_doc = '[0, 1, 2, 3]';
SELECT JSON_ARRAY_APPEND(@json_doc, null, 4); Risultato:
+---------------------------------------+ | JSON_ARRAY_APPEND(@json_doc, null, 4) | +---------------------------------------+ | NULL | +---------------------------------------+
Tuttavia, se il value l'argomento è NULL , quindi NULL viene aggiunto all'array:
SET @json_doc = '[0, 1, 2, 3]';
SELECT JSON_ARRAY_APPEND(@json_doc, '$', null); Risultato:
+-----------------------------------------+ | JSON_ARRAY_APPEND(@json_doc, '$', null) | +-----------------------------------------+ | [0, 1, 2, 3, null] | +-----------------------------------------+
Puoi anche usare JSON_ARRAY_INSERT() per inserire valori in una matrice.