Purpose
This function checks whether one or more specified paths are contained in the target JSON document. If they are, the function returns 1; otherwise, it returns 0.
Syntax
JSON_CONTAINS_PATH(json_doc, one_or_all, path[, path] ...)
Description
The json_doc parameter specifies the JSON document, and path is a path parameter. If either parameter is NULL, NULL is returned.
The valid values of the one_or_all parameter are as follows:
'one': Returns 1 if the document contains at least one path; otherwise, returns 0.'all': Returns 1 if all paths exist in the document, otherwise returns 0.
An error will be returned in the following scenarios:
The
json_docparameter is not a valid JSON document.Any
pathparameter is not a valid path expression.The value of the
one_or_allparameter is not'one'or'all'.
Examples
obclient> SET @jn = '{"a": 1, "b": 2, "c": {"d": 4}}';
Query OK, 0 rows affected
obclient> SELECT JSON_CONTAINS_PATH(@jn, 'one', '$.a', '$.e');
+---------------------------------------------+
| JSON_CONTAINS_PATH(@j, 'one', '$.a', '$.e') |
+---------------------------------------------+
| 1 |
+---------------------------------------------+
1 row in set
obclient> SELECT JSON_CONTAINS_PATH(@jn, 'all', '$.a', '$.e');
+---------------------------------------------+
| JSON_CONTAINS_PATH(@j, 'all', '$.a', '$.e') |
+---------------------------------------------+
| 0 |
+---------------------------------------------+
1 row in set
obclient> SELECT JSON_CONTAINS_PATH(@jn, 'one', '$.c.d');
+----------------------------------------+
| JSON_CONTAINS_PATH(@j, 'one', '$.c.d') |
+----------------------------------------+
| 1 |
+----------------------------------------+
1 row in set
obclient> SELECT JSON_CONTAINS_PATH(@jn, 'one', '$.a.d');
+----------------------------------------+
| JSON_CONTAINS_PATH(@j, 'one', '$.a.d') |
+----------------------------------------+
| 0 |
+----------------------------------------+
1 row in set