In MariaDB, JSON_INSERT() è una funzione incorporata che inserisce i dati in un documento JSON e restituisce il risultato.
Sintassi
La sintassi è questa:
JSON_INSERT(json_doc, path, val[, path, val] ...)
Dove json_doc è il documento JSON, path è il percorso in cui inserire i dati e val è il valore da inserire in quel percorso.
Esempio
Ecco un esempio da dimostrare.
SET @json_document = '
{
"name": "Wag"
}
';
SELECT JSON_INSERT(@json_document, '$.type', "Dog"); Risultato:
+----------------------------------------------+
| JSON_INSERT(@json_document, '$.type', "Dog") |
+----------------------------------------------+
| {"name": "Wag", "type": "Dog"} |
+----------------------------------------------+
Qui ho inserito "type": "Dog" nel documento.
In questo caso, ho usato $.type come il sentiero. Pertanto, type è la chiave e Dog è il valore.
Quando il percorso esiste già
Il passaggio di un percorso già esistente nel documento JSON comporta la restituzione del documento originale invariato.
Esempio:
SET @json_document = '
{
"name": "Wag"
}
';
SELECT JSON_INSERT(@json_document, '$.name', "Bark"); Risultato:
+-----------------------------------------------+
| JSON_INSERT(@json_document, '$.name', "Bark") |
+-----------------------------------------------+
| {"name": "Wag"} |
+-----------------------------------------------+ Inserimento di array
Ecco un esempio di inserimento di un array in un documento JSON:
SET @json_document = '
{
"name" : "Wag"
}
';
SELECT JSON_INSERT(@json_document, '$.scores', '[ 8, 7, 9 ]'); Risultato:
+--------------------------------------------------------+
| JSON_INSERT(@json_document, '$.scores', '[ 8, 7, 9 ]') |
+--------------------------------------------------------+
| {"name": "Wag", "scores": "[ 8, 7, 9 ]"} |
+--------------------------------------------------------+ Aggiungere agli array
Ecco un esempio di utilizzo di JSON_INSERT() per aggiungere dati a un array:
SET @json_document = '
{
"_id" : 1,
"awards" : [ "Top Dog", "Best Dog" ]
}
';
SELECT JSON_INSERT(@json_document, '$.awards[2]', "Biggest Dog"); Risultato:
+--------------------------------------------------------------+
| JSON_INSERT(@json_document, '$.awards[2]', "Biggest Dog") |
+--------------------------------------------------------------+
| {"_id": 1, "awards": ["Top Dog", "Best Dog", "Biggest Dog"]} |
+--------------------------------------------------------------+ Tuttavia, sebbene abbia funzionato bene per questo esempio, avrebbe potuto facilmente fallire. Ad esempio, se proviamo a inserire il valore in un punto diverso dell'array, non funziona:
SET @json_document = '
{
"_id" : 1,
"awards" : [ "Top Dog", "Best Dog" ]
}
';
SELECT JSON_INSERT(@json_document, '$.awards[1]', "Biggest Dog"); Risultato:
+-----------------------------------------------------------+
| JSON_INSERT(@json_document, '$.awards[1]', "Biggest Dog") |
+-----------------------------------------------------------+
| {"_id": 1, "awards": ["Top Dog", "Best Dog"]} |
+-----------------------------------------------------------+
Per inserire valori in una matrice, usa JSON_ARRAY_INSERT() funzione invece.
Inoltre, sebbene siamo stati in grado di aggiungere un valore a un array nell'esempio sopra, probabilmente è meglio usare JSON_ARRAY_APPEND() funzione, poiché è progettato specificamente per tale scopo.
Oggetti nidificati
Ecco un esempio di inserimento di un valore in un oggetto nidificato all'interno di un altro oggetto:
SET @json_document = '
{
"_id" : 1,
"name" : "Wag",
"details" : {
"type" : "Dog",
"weight" : 20,
"awards" : {
"Florida Dog Awards" : "Top Dog",
"New York Marathon" : "Fastest Dog"
}
}
}
';
SELECT JSON_INSERT(
@json_document,
'$.details.awards.Sumo 2020',
'Biggest Dog'
); Risultato:
{"_id": 1, "name": "Wag", "details": {"type": "Dog", "weight": 20, "awards": {"Florida Dog Awards": "Top Dog", "New York Marathon": "Fastest Dog", "Sumo 2020": "Biggest Dog"}}} Abbellire il risultato
Possiamo usare il JSON_DETAILED() funzione per facilitare la lettura del risultato:
SET @json_document = '
{
"_id" : 1,
"name" : "Wag",
"details" : {
"type" : "Dog",
"weight" : 20,
"awards" : {
"Florida Dog Awards" : "Top Dog",
"New York Marathon" : "Fastest Dog"
}
}
}
';
SELECT
JSON_DETAILED(
JSON_INSERT(
@json_document,
'$.details.awards.Sumo 2020',
'Biggest Dog'
)
); Risultato:
{
"_id": 1,
"name": "Wag",
"details":
{
"type": "Dog",
"weight": 20,
"awards":
{
"Florida Dog Awards": "Top Dog",
"New York Marathon": "Fastest Dog",
"Sumo 2020": "Biggest Dog"
}
}
} Argomenti nulli
Se uno dei json_document o path gli argomenti sono NULL , il risultato è NULL :
SELECT
JSON_INSERT(null, '$.type', 'Dog'),
JSON_INSERT('{"a":1}', null, 'Dog'); Risultato:
+------------------------------------+-------------------------------------+
| JSON_INSERT(null, '$.type', 'Dog') | JSON_INSERT('{"a":1}', null, 'Dog') |
+------------------------------------+-------------------------------------+
| NULL | NULL |
+------------------------------------+-------------------------------------+
Tuttavia, se il value l'argomento è NULL , la chiave viene aggiunta al percorso specificato, con un valore di null :
SELECT JSON_INSERT('{"a":1}', '$.type', null); Risultato:
+----------------------------------------+
| JSON_INSERT('{"a":1}', '$.type', null) |
+----------------------------------------+
| {"a": 1, "type": null} |
+----------------------------------------+ Conteggio parametri errato
Non fornire argomenti genera un errore:
SELECT JSON_INSERT(); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_INSERT'
È lo stesso quando fornisci troppi pochi o troppi argomenti:
SELECT JSON_INSERT('{ "a": 1}'); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_INSERT'
Funzioni simili
Il JSON_REPLACE() la funzione può aggiornare i dati esistenti.
Il JSON_SET() la funzione può aggiornare i dati esistenti e inserire nuovi dati. Quindi JSON_SET() è come JSON_INSERT() e JSON_REPLACE() in una funzione.