In MariaDB, JSON_CONTAINS() è una funzione integrata che consente di scoprire se un valore specificato è stato trovato nel documento JSON specificato o in un percorso specifico all'interno del documento.
Restituisce 1 se contiene il valore, 0 in caso contrario, e NULL se uno qualsiasi degli argomenti è NULL .
Sintassi
La sintassi è questa:
JSON_CONTAINS(json_doc, val[, path])
Dove json_doc è il documento JSON, val è il valore da trovare e path un valore facoltativo che specifica un percorso all'interno del documento.
Esempio
Ecco un esempio da dimostrare.
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, '{"name": "Wag"}'); Risultato:
+--------------------------------------------------+
| JSON_CONTAINS(@json_document, '{"name": "Wag"}') |
+--------------------------------------------------+
| 1 |
+--------------------------------------------------+
In questo caso, c'è stata una corrispondenza e il risultato è 1 .
Nel prossimo esempio, non c'è corrispondenza:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, '{"name": "Woof"}'); Risultato:
+---------------------------------------------------+
| JSON_CONTAINS(@json_document, '{"name": "Woof"}') |
+---------------------------------------------------+
| 0 |
+---------------------------------------------------+ Si noti che il valore è racchiuso tra parentesi graffe.
Ecco cosa succede quando il secondo argomento non è valido:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, 'Wag'); Risultato:
+--------------------------------------+ | JSON_CONTAINS(@json_document, 'Wag') | +--------------------------------------+ | NULL | +--------------------------------------+ 1 row in set, 1 warning (0.000 sec)
Vediamo l'avviso:
SHOW WARNINGS; Risultato:
+---------+------+-----------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------------------------------------------------+ | Warning | 4038 | Syntax error in JSON text in argument 2 to function 'json_contains' at position 1 | +---------+------+-----------------------------------------------------------------------------------+
Specifica un percorso
È possibile utilizzare facoltativamente un terzo argomento per specificare un percorso.
Esempio:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, 10, '$.weight'); Risultato:
+-----------------------------------------------+ | JSON_CONTAINS(@json_document, 10, '$.weight') | +-----------------------------------------------+ | 1 | +-----------------------------------------------+
Quando si specifica un percorso, non è stato necessario utilizzare parentesi graffe.
Eccone uno che cerca una stringa:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, '"Wag"', '$.name'); Risultato:
+--------------------------------------------------+ | JSON_CONTAINS(@json_document, '"Wag"', '$.name') | +--------------------------------------------------+ | 1 | +--------------------------------------------------+
Si noti che ho usato virgolette doppie all'interno delle virgolette singole. Se ometto le virgolette, ecco cosa succede:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, 'Wag', '$.name'); Risultato:
+------------------------------------------------+ | JSON_CONTAINS(@json_document, 'Wag', '$.name') | +------------------------------------------------+ | NULL | +------------------------------------------------+ 1 row in set, 1 warning (0.000 sec)
E controlliamo l'avviso:
SHOW WARNINGS; Risultato:
+---------+------+-----------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------------------------------------------------+ | Warning | 4038 | Syntax error in JSON text in argument 2 to function 'json_contains' at position 1 | +---------+------+-----------------------------------------------------------------------------------+
Strutture nidificate
Ecco un esempio che cerca un valore all'interno di un documento nidificato:
SET @json_document = '{ "name": "Wag", "specs": { "weight": 10, "height": 30 } }';
SELECT JSON_CONTAINS(@json_document, 30, '$.specs.height'); Risultato:
+-----------------------------------------------------+ | JSON_CONTAINS(@json_document, 30, '$.specs.height') | +-----------------------------------------------------+ | 1 | +-----------------------------------------------------+
Argomenti nulli
Se uno qualsiasi degli argomenti è NULL , il risultato è NULL :
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT
JSON_CONTAINS(null, 10, '$.weight') AS a,
JSON_CONTAINS(@json_document, null, '$.weight') AS b,
JSON_CONTAINS(@json_document, 10, null) AS c; Risultato:
+------+------+------+ | a | b | c | +------+------+------+ | NULL | NULL | NULL | +------+------+------+
Conteggio parametri errato
Non fornire argomenti genera un errore:
SELECT JSON_CONTAINS(); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_CONTAINS'
È lo stesso quando fornisci troppi argomenti:
SELECT JSON_CONTAINS('{ "a": 1}', 1, 2, 3); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_CONTAINS'