In MariaDB, JSON_QUERY() è una funzione incorporata che restituisce un oggetto o un array da un documento JSON, in base al percorso fornito.
È simile a JSON_VALUE() funzione, tranne per il fatto che restituisce un oggetto o un array invece di uno scalare (JSON_VALUE() restituisce uno scalare).
Sintassi
La sintassi è questa:
JSON_QUERY(json_doc, path)
Dove json_doc è il documento JSON e path è un percorso all'interno del documento.
Esempio
Ecco un esempio da dimostrare.
SET @json_document = '
{
"_id" : 1,
"name" : "Wag",
"details" : {
"type" : "Dog",
"weight" : 20,
"awards" : {
"Florida Dog Awards" : "Top Dog",
"New York Marathon" : "Fastest Dog",
"Sumo 2020" : "Biggest Dog"
}
}
}
';
SELECT JSON_QUERY(@json_document, '$.details'); Risultato:
{
"type" : "Dog",
"weight" : 20,
"awards" : {
"Florida Dog Awards" : "Top Dog",
"New York Marathon" : "Fastest Dog",
"Sumo 2020" : "Biggest Dog"
}
} Possiamo usare la notazione del punto per passare al prossimo oggetto nidificato:
SET @json_document = '
{
"_id" : 1,
"name" : "Wag",
"details" : {
"type" : "Dog",
"weight" : 20,
"awards" : {
"Florida Dog Awards" : "Top Dog",
"New York Marathon" : "Fastest Dog",
"Sumo 2020" : "Biggest Dog"
}
}
}
';
SELECT JSON_QUERY(@json_document, '$.details.awards'); Risultato:
{
"Florida Dog Awards" : "Top Dog",
"New York Marathon" : "Fastest Dog",
"Sumo 2020" : "Biggest Dog"
} Array
Ecco un esempio di restituzione di un array:
SET @json_document = '
{
"_id" : 1,
"awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ]
}
';
SELECT JSON_QUERY(@json_document, '$.awards'); Risultato:
+------------------------------------------+ | JSON_QUERY(@json_document, '$.awards') | +------------------------------------------+ | [ "Top Dog", "Best Dog", "Biggest Dog" ] | +------------------------------------------+
Se vuoi restituire un vero elemento dell'array, prova JSON_VALUE() funzione.
Percorso inesistente
Il passaggio di un percorso che non esiste nel documento JSON restituisce NULL .
Esempio:
SET @json_document = '
{
"_id" : 1,
"awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ]
}
';
SELECT JSON_QUERY(@json_document, '$.type'); Risultato:
+--------------------------------------+ | JSON_QUERY(@json_document, '$.type') | +--------------------------------------+ | NULL | +--------------------------------------+
Valori scalari
Il tentativo di restituire un valore scalare restituisce NULL .
Esempio:
SELECT JSON_QUERY('{ "weight": 10 }', '$.weight'); Risultato:
+--------------------------------------------+
| JSON_QUERY('{ "weight": 10 }', '$.weight') |
+--------------------------------------------+
| NULL |
+--------------------------------------------+
Per restituire un valore scalare, usa JSON_VALUE() funzione.
Argomenti nulli
Se un argomento è NULL , il risultato è NULL :
SELECT
JSON_QUERY(null, '$.type'),
JSON_QUERY('{"a":1}', null); Risultato:
+----------------------------+-----------------------------+
| JSON_QUERY(null, '$.type') | JSON_QUERY('{"a":1}', null) |
+----------------------------+-----------------------------+
| NULL | NULL |
+----------------------------+-----------------------------+ Conteggio parametri errato
Non fornire argomenti genera un errore:
SELECT JSON_QUERY(); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_QUERY'
È lo stesso quando fornisci troppi pochi o troppi argomenti:
SELECT JSON_QUERY('{ "a": 1}'); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_QUERY'