En MariaDB, JSON_CONTAINS_PATH() es una función integrada que indica si un documento JSON determinado contiene datos en la ruta o rutas especificadas.
Devuelve 1 si el documento contiene datos en las rutas especificadas, 0 si no es así, y NULL si alguno de los argumentos es NULL .
Sintaxis
La sintaxis es así:
JSON_CONTAINS_PATH(json_doc, return_arg, path[, path] ...)
Donde json_doc es el documento JSON y path especifica la ruta para la cual buscar datos. Se pueden proporcionar varias rutas.
El return_arg argumento determina cómo tratar con múltiples caminos. Puede ser one o all .
one– La función devuelve1si existe al menos una ruta dentro del documento JSON.all– La función devuelve1solo si todas las rutas existen dentro del documento JSON.
Ejemplo
Aquí hay un ejemplo para demostrarlo.
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS_PATH(@json_document, 'one', '$.name'); Resultado:
+-----------------------------------------------------+ | JSON_CONTAINS_PATH(@json_document, 'one', '$.name') | +-----------------------------------------------------+ | 1 | +-----------------------------------------------------+
En este caso, la ruta existe y el resultado es 1 .
En el siguiente ejemplo, la ruta no existe y el resultado es 0 :
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS_PATH(@json_document, 'one', '$.type'); Resultado:
+-----------------------------------------------------+ | JSON_CONTAINS_PATH(@json_document, 'one', '$.type') | +-----------------------------------------------------+ | 0 | +-----------------------------------------------------+
Vías Múltiples
Estos son algunos ejemplos que buscan múltiples rutas dentro del documento:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS_PATH(
@json_document,
'one',
'$.type',
'$.weight'
) AS Result; Resultado:
+--------+ | Result | +--------+ | 1 | +--------+
En este ejemplo buscamos dos caminos. Un camino existe y el otro no. Pero obtuvimos un 1 de todos modos (un resultado positivo). Esto se debe a que usamos one como segundo argumento. El one especifica que obtendremos un 1 si alguna de las rutas existe.
Esto es lo que sucede si usamos all como segundo argumento:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS_PATH(
@json_document,
'all',
'$.type',
'$.weight'
) AS Result; Resultado:
+--------+ | Result | +--------+ | 0 | +--------+
Esta vez el resultado es 0 , porque no todas las rutas existen en el documento JSON.
Si cambiamos la ruta que falta ($.type ) a uno que existe, obtenemos un resultado diferente:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS_PATH(
@json_document,
'all',
'$.name',
'$.weight'
) AS Result; Resultado:
+--------+ | Result | +--------+ | 1 | +--------+
Arreglos
Aquí hay un ejemplo que prueba si un índice dado existe dentro de una matriz:
SET @json_document = '
{
"name": "Wag",
"awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ]
}
';
SELECT JSON_CONTAINS_PATH(
@json_document,
'one',
'$.awards[2]'
) AS Result; Resultado:
+--------+ | Result | +--------+ | 1 | +--------+
Y esto es lo que sucede si aumentamos el índice a uno inexistente:
SET @json_document = '
{
"name": "Wag",
"awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ]
}
';
SELECT JSON_CONTAINS_PATH(
@json_document,
'one',
'$.awards[3]'
) AS Result; Resultado:
+--------+ | Result | +--------+ | 0 | +--------+
Estructuras anidadas
Aquí hay un ejemplo que busca una ruta dentro de un documento anidado:
SET @json_document = '
{
"name": "Wag",
"specs": {
"weight": 10,
"height": 30
}
}
';
SELECT JSON_CONTAINS_PATH(
@json_document,
'one',
'$.specs.height'
) AS Result; Resultado:
+--------+ | Result | +--------+ | 1 | +--------+
Argumentos nulos
Si algún argumento es NULL , el resultado es NULL :
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT
JSON_CONTAINS_PATH(null, 'one', '$.weight') AS a,
JSON_CONTAINS_PATH(@json_document, null, '$.weight') AS b,
JSON_CONTAINS_PATH(@json_document, 'one', 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_PATH(); Resultado:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_CONTAINS_PATH'
Lo mismo sucede cuando no pasa suficientes argumentos:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS_PATH(@json_document); Resultado:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_CONTAINS_PATH'