En MariaDB, JSON_CONTAINS()
es una función integrada que le permite averiguar si un valor específico se encuentra en el documento JSON dado o en una ruta específica dentro del documento.
Devuelve 1
si contiene el valor, 0
si no es así, y NULL
si alguno de los argumentos es NULL
.
Sintaxis
La sintaxis es así:
JSON_CONTAINS(json_doc, val[, path])
Donde json_doc
es el documento JSON, val
es el valor a buscar, y path
un valor opcional que especifica una ruta dentro del documento.
Ejemplo
Aquí hay un ejemplo para demostrarlo.
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, '{"name": "Wag"}');
Resultado:
+--------------------------------------------------+ | JSON_CONTAINS(@json_document, '{"name": "Wag"}') | +--------------------------------------------------+ | 1 | +--------------------------------------------------+
En este caso, hubo una coincidencia y el resultado es 1
.
En el siguiente ejemplo, no hay coincidencia:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, '{"name": "Woof"}');
Resultado:
+---------------------------------------------------+ | JSON_CONTAINS(@json_document, '{"name": "Woof"}') | +---------------------------------------------------+ | 0 | +---------------------------------------------------+
Tenga en cuenta que el valor está encerrado entre llaves.
Esto es lo que sucede cuando el segundo argumento no es válido:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, 'Wag');
Resultado:
+--------------------------------------+ | JSON_CONTAINS(@json_document, 'Wag') | +--------------------------------------+ | NULL | +--------------------------------------+ 1 row in set, 1 warning (0.000 sec)
Veamos la advertencia:
SHOW WARNINGS;
Resultado:
+---------+------+-----------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------------------------------------------------+ | Warning | 4038 | Syntax error in JSON text in argument 2 to function 'json_contains' at position 1 | +---------+------+-----------------------------------------------------------------------------------+
Especifique una ruta
Opcionalmente, puede usar un tercer argumento para especificar una ruta.
Ejemplo:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, 10, '$.weight');
Resultado:
+-----------------------------------------------+ | JSON_CONTAINS(@json_document, 10, '$.weight') | +-----------------------------------------------+ | 1 | +-----------------------------------------------+
Al especificar una ruta, no necesitaba usar llaves.
Aquí hay uno que busca una cadena:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, '"Wag"', '$.name');
Resultado:
+--------------------------------------------------+ | JSON_CONTAINS(@json_document, '"Wag"', '$.name') | +--------------------------------------------------+ | 1 | +--------------------------------------------------+
Observe que usé comillas dobles dentro de las comillas simples. Si omito las comillas dobles, esto es lo que sucede:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, 'Wag', '$.name');
Resultado:
+------------------------------------------------+ | JSON_CONTAINS(@json_document, 'Wag', '$.name') | +------------------------------------------------+ | NULL | +------------------------------------------------+ 1 row in set, 1 warning (0.000 sec)
Y veamos la advertencia:
SHOW WARNINGS;
Resultado:
+---------+------+-----------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------------------------------------------------+ | Warning | 4038 | Syntax error in JSON text in argument 2 to function 'json_contains' at position 1 | +---------+------+-----------------------------------------------------------------------------------+
Estructuras anidadas
Aquí hay un ejemplo que busca un valor dentro de un documento anidado:
SET @json_document = '{ "name": "Wag", "specs": { "weight": 10, "height": 30 } }';
SELECT JSON_CONTAINS(@json_document, 30, '$.specs.height');
Resultado:
+-----------------------------------------------------+ | JSON_CONTAINS(@json_document, 30, '$.specs.height') | +-----------------------------------------------------+ | 1 | +-----------------------------------------------------+
Argumentos nulos
Si alguno de los argumentos es NULL
, el resultado es 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;
Resultado:
+------+------+------+ | a | b | c | +------+------+------+ | NULL | NULL | NULL | +------+------+------+
Recuento de parámetros incorrecto
No proporcionar argumentos da como resultado un error:
SELECT JSON_CONTAINS();
Resultado:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_CONTAINS'
Es lo mismo cuando proporciona demasiados argumentos:
SELECT JSON_CONTAINS('{ "a": 1}', 1, 2, 3);
Resultado:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_CONTAINS'