MariaDB
 SQL >> Database >  >> RDS >> MariaDB

Leggere valori scalari dal JSON in MariaDB con JSON_VALUE()

JSON_VALUE() recupera un singolo valore scalare da un documento JSON. Indichi il percorso e lui restituisce qualunque scalare si trovi lì—una stringa, un numero, un booleano o un null JSON.

Sintassi

JSON_VALUE(json_doc, path)

json_doc è il documento e path è la posizione al suo interno.

Una ricerca semplice

SET @json_document = ' { "name": "Wag", "type": "Dog", "weight": 20 } ';
SELECT JSON_VALUE(@json_document, '$.name');

Risultato:

+--------------------------------------+
| JSON_VALUE(@json_document, '$.name') |
+--------------------------------------+
| Wag                                  |
+--------------------------------------+

Percorsi mancanti

SET @json_document = ' { "name": "Wag", "type": "Dog", "weight": 20 } ';
SELECT JSON_VALUE(@json_document, '$.color');

Risultato:

+---------------------------------------+
| JSON_VALUE(@json_document, '$.color') |
+---------------------------------------+
| NULL                                  |
+---------------------------------------+

Elementi di 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 partono da zero, quindi $.awards[1] è il secondo elemento dell'array awards.

Oggetti annidati

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 |
+---------+

I valori strutturati restituiscono NULL

Se chiedi un oggetto o un array otterrai NULL:

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 oggetti e array, rivolgiti invece a JSON_QUERY() o JSON_EXTRACT().

Argomenti NULL

SELECT JSON_VALUE(null, '$.type'), JSON_VALUE('{"a":1}', null);

Risultato:

+----------------------------+-----------------------------+
| JSON_VALUE(null, '$.type') | JSON_VALUE('{"a":1}', null) |
+----------------------------+-----------------------------+
| NULL                       | NULL                        |
+----------------------------+-----------------------------+

Errori nel numero di argomenti

SELECT JSON_VALUE();

Risultato:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_VALUE'

Troppo pochi argomenti produce lo stesso messaggio:

SELECT JSON_VALUE('{ "a": 1}');

Risultato:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_VALUE'