In MariaDB, JSON_VALUE() è una funzione incorporata che restituisce un valore scalare da un documento JSON. Più precisamente, restituisce lo scalare specificato dal percorso fornito.
Sintassi
La sintassi è questa:
JSON_VALUE(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 = '
{
"name": "Wag",
"type": "Dog",
"weight": 20
}
';
SELECT JSON_VALUE(@json_document, '$.name'); Risultato:
+--------------------------------------+ | JSON_VALUE(@json_document, '$.name') | +--------------------------------------+ | Wag | +--------------------------------------+
Percorso inesistente
Il passaggio di un percorso che non esiste nel documento JSON restituisce NULL .
Esempio:
SET @json_document = '
{
"name": "Wag",
"type": "Dog",
"weight": 20
}
';
SELECT JSON_VALUE(@json_document, '$.color'); Risultato:
+---------------------------------------+ | JSON_VALUE(@json_document, '$.color') | +---------------------------------------+ | NULL | +---------------------------------------+
Array
Ecco un esempio di restituzione di dati da un array:
SET @json_document = '
{
"_id" : 1,
"awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ]
}
';
SELECT JSON_VALUE(@json_document, '$.awards[1]'); Risultato:
+-------------------------------------------+ | JSON_VALUE(@json_document, '$.awards[1]') | +-------------------------------------------+ | Best Dog | +-------------------------------------------+
Gli array sono a base zero, quindi $.awards[1] estrae il secondo elemento dei awards matrice.
Oggetti nidificati
Ecco un esempio di come ottenere un valore da un oggetto annidato 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",
"Sumo 2020" : "Biggest Dog"
}
}
}
';
SELECT JSON_VALUE(
@json_document,
'$.details.awards.Florida Dog Awards'
) AS Result; Risultato:
+---------+ | Result | +---------+ | Top Dog | +---------+
Valori non scalari
Il tentativo di restituire un valore non scalare (ad esempio un oggetto o un array) restituisce NULL .
Esempio:
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_VALUE(
@json_document,
'$.details.awards'
) AS Result; Risultato:
+--------+ | Result | +--------+ | NULL | +--------+
Per restituire un valore non scalare, utilizza JSON_QUERY() o la funzione JSON_EXTRACT() funzione.
Argomenti nulli
Se un argomento è NULL , il risultato è NULL :
SELECT
JSON_VALUE(null, '$.type'),
JSON_VALUE('{"a":1}', null); Risultato:
+----------------------------+-----------------------------+
| JSON_VALUE(null, '$.type') | JSON_VALUE('{"a":1}', null) |
+----------------------------+-----------------------------+
| NULL | NULL |
+----------------------------+-----------------------------+ Conteggio parametri errato
Non fornire argomenti genera un errore:
SELECT JSON_VALUE(); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_VALUE'
È lo stesso quando fornisci troppi pochi o troppi argomenti:
SELECT JSON_VALUE('{ "a": 1}'); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_VALUE'