Purpose
This function checks whether one or more specified paths are present in the target JSON document. It returns 1 if the paths are present, and 0 otherwise.
Syntax
JSON_CONTAINS_PATH(json_doc, one_or_all, path[, path] ...)
Considerations
The json_doc parameter specifies the JSON document, and path specifies the path. If any parameter is NULL, the function returns NULL.
The one_or_all parameter can take the following values:
'one': Returns 1 if at least one path exists in the document, otherwise returns 0.'all': Returns 1 if all paths exist in the document, otherwise returns 0.
The following situations will result in an error:
The
json_docparameter is not a valid JSON document.Any
pathparameter is not a valid path expression.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
