In MariaDB, JSON_SET() è una funzione integrata che aggiorna o inserisce dati in un documento JSON e restituisce il risultato.
JSON_SET() può aggiornare e inserire dati, mentre JSON_INSERT() può solo inserire dati e JSON_REPLACE() può solo aggiornare i dati.
Sintassi
La sintassi è questa:
JSON_SET(json_doc, path, val[, path, val] ...) Dove:
json_docè il documento JSON.pathè il percorso dell'elemento per il quale inserire dati o aggiornare il valore in.valè il nuovo valore.
Esempio
Ecco un esempio da dimostrare.
SET @json = '
{
"name" : "Wag",
"type" : "Cat"
}
';
SELECT JSON_SET(@json, '$.type', 'Dog'); Risultato:
+----------------------------------+
| JSON_SET(@json, '$.type', 'Dog') |
+----------------------------------+
| {"name": "Wag", "type": "Dog"} |
+----------------------------------+
In questo caso ho aggiornato il valore del type membro Cat a Dog .
Elementi della matrice
Per aggiornare o inserire un elemento dell'array, specifica l'indice dell'elemento:
SET @json = '
{
"name" : "Wag",
"scores" : [8, 0, 9]
}
';
SELECT JSON_SET(@json, '$.scores[1]', 7); Risultato:
+--------------------------------------+
| JSON_SET(@json, '$.scores[1]', 7) |
+--------------------------------------+
| {"name": "Wag", "scores": [8, 7, 9]} |
+--------------------------------------+
In questo caso, il secondo elemento dell'array è stato aggiornato con il nuovo valore. Gli array sono a base zero, quindi $.scores[1] fa riferimento al secondo elemento nell'array.
Inserisci dati
Gli esempi precedenti hanno aggiornato i dati esistenti. Ecco un esempio di inserimento di nuovi dati:
SET @json = '
{
"name" : "Wag",
"type" : "Dog"
}
';
SELECT JSON_SET(@json, '$.weight', 10); Risultato:
+----------------------------------------------+
| JSON_SET(@json, '$.weight', 10) |
+----------------------------------------------+
| {"name": "Wag", "type": "Dog", "weight": 10} |
+----------------------------------------------+
Qui, abbiamo inserito una nuova coppia chiave/valore ("weight": 10 ).
Di seguito è riportato un esempio di aggiunta di un nuovo elemento a un array:
SET @json = '
{
"name" : "Wag",
"scores" : [8, 0, 9]
}
';
SELECT JSON_SET(@json, '$.scores[3]', 7); Risultato:
+-----------------------------------------+
| JSON_SET(@json, '$.scores[3]', 7) |
+-----------------------------------------+
| {"name": "Wag", "scores": [8, 0, 9, 7]} |
+-----------------------------------------+
Questo esempio di array può essere eseguito anche con JSON_ARRAY_INSERT() o JSON_ARRAY_APPEND() .
Percorsi multipli
La sintassi consente di impostare/aggiornare valori su più percorsi con una singola chiamata a JSON_SET() .
Esempio:
SET @json = '
{
"name" : "Scratch",
"type" : "Rat"
}
';
SELECT JSON_SET(@json, '$.type', 'Cat', '$.weight', 10); Risultato:
+--------------------------------------------------+
| JSON_SET(@json, '$.type', 'Cat', '$.weight', 10) |
+--------------------------------------------------+
| {"name": "Scratch", "type": "Cat", "weight": 10} |
+--------------------------------------------------+ Nel prossimo esempio inseriamo un nuovo valore in un array e ne aggiorniamo un altro:
SET @json = '{ "scores" : [ 0, 1, 2 ] }';
SELECT
JSON_SET(
@json,
'$.scores[1]', "a",
'$.scores[3]', "b"
) AS Result; Risultato:
+------------------------------+
| Result |
+------------------------------+
| {"scores": [0, "a", 2, "b"]} |
+------------------------------+ Argomenti nulli
Se il json_doc o uno qualsiasi dei path argomenti è null , il risultato è NULL . Ma se il value l'argomento è null , quindi il valore viene sostituito con null .
SELECT
JSON_SET(null, '$.a', 1) AS a,
JSON_SET('{"a":1}', null, 1) AS b,
JSON_SET('{"a":1}', '$.a', null) AS c; Risultato:
+------+------+-------------+
| a | b | c |
+------+------+-------------+
| NULL | NULL | {"a": null} |
+------+------+-------------+
Nell'esempio seguente, uno degli argomenti del percorso manca da ogni chiamata a JSON_SET() . In entrambi i casi, il risultato è NULL :
SET @json = '
{
"name" : "Wag",
"type" : "Cat"
}
';
SELECT
JSON_SET(@json, null, 'Bark', '$.type', 'Dog') AS a,
JSON_SET(@json, '$.name', 'Bark', null, 'Dog') AS b; Risultato:
+------+------+ | a | b | +------+------+ | NULL | NULL | +------+------+
Conteggio parametri errato
Chiamando JSON_SET() senza un argomento genera un errore:
SELECT JSON_SET(); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_SET'
È lo stesso quando non vengono passati argomenti sufficienti:
SELECT JSON_SET('{"a":1}'); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_SET'