No MariaDB,
JSON_CONTAINS() é uma função interna que permite descobrir se um valor especificado é encontrado no documento JSON fornecido ou em um caminho específico dentro do documento. Ele retorna
1 se contiver o valor, 0 se não, e NULL se algum dos argumentos for NULL . Sintaxe
A sintaxe fica assim:
JSON_CONTAINS(json_doc, val[, path]) Onde
json_doc é o documento JSON, val é o valor a ser encontrado e path um valor opcional que especifica um caminho dentro do documento. Exemplo
Aqui está um exemplo para demonstrar.
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, '{"name": "Wag"}'); Resultado:
+--------------------------------------------------+
| JSON_CONTAINS(@json_document, '{"name": "Wag"}') |
+--------------------------------------------------+
| 1 |
+--------------------------------------------------+ Nesse caso, houve uma correspondência e o resultado é
1 . No próximo exemplo, não há correspondência:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, '{"name": "Woof"}'); Resultado:
+---------------------------------------------------+
| JSON_CONTAINS(@json_document, '{"name": "Woof"}') |
+---------------------------------------------------+
| 0 |
+---------------------------------------------------+ Observe que o valor está entre chaves.
Aqui está o que acontece quando o segundo argumento não é 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)
Vejamos o aviso:
SHOW WARNINGS; Resultado:
+---------+------+-----------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------------------------------------------------+ | Warning | 4038 | Syntax error in JSON text in argument 2 to function 'json_contains' at position 1 | +---------+------+-----------------------------------------------------------------------------------+
Especifique um caminho
Opcionalmente, você pode usar um terceiro argumento para especificar um caminho.
Exemplo:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, 10, '$.weight'); Resultado:
+-----------------------------------------------+ | JSON_CONTAINS(@json_document, 10, '$.weight') | +-----------------------------------------------+ | 1 | +-----------------------------------------------+
Ao especificar um caminho, não precisei usar chaves.
Aqui está um procurando por uma string:
SET @json_document = '{ "name": "Wag", "weight": 10 }';
SELECT JSON_CONTAINS(@json_document, '"Wag"', '$.name'); Resultado:
+--------------------------------------------------+ | JSON_CONTAINS(@json_document, '"Wag"', '$.name') | +--------------------------------------------------+ | 1 | +--------------------------------------------------+
Observe que usei aspas duplas dentro das aspas simples. Se eu omitir as aspas duplas, eis o que acontece:
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)
E vamos verificar o aviso:
SHOW WARNINGS; Resultado:
+---------+------+-----------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------------------------------------------------+ | Warning | 4038 | Syntax error in JSON text in argument 2 to function 'json_contains' at position 1 | +---------+------+-----------------------------------------------------------------------------------+
Estruturas aninhadas
Aqui está um exemplo que procura um valor em um documento aninhado:
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
Se algum dos argumentos for
NULL , o resultado é 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 | +------+------+------+
Contagem de parâmetros incorreta
Não fornecer argumentos resulta em um erro:
SELECT JSON_CONTAINS(); Resultado:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_CONTAINS'
É o mesmo quando você fornece muitos argumentos:
SELECT JSON_CONTAINS('{ "a": 1}', 1, 2, 3); Resultado:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_CONTAINS'