Purpose
JSON_SCHEMA_VALID() validates a JSON document. If the JSON document validates against the specified JSON schema, the function returns 1. Otherwise, the function returns 0. You can use this function on a column constraint.
Syntax
JSON_SCHEMA_VALID(schema, document)
Description
The
schemaargument specifies a JSON schema, which must be a valid JSON object.JSON schemas support the
requiredattribute to enforce the inclusion of required properties.JSON schemas support the
id,$schema,description, andtypeattributes but do not require any of these.
The
documentargument specifies the JSON document to be validated. The JSON document must be a valid JSON document.If any of the arguments is not valid JSON, the function raises an error.
Examples
The following example defines the JSON schema '{"type": "string"}', which requires that valid JSON data be of the string type. '"JSON_doc"' is the JSON document to be validated.
Notice
In this statement, the JSON_doc string is wrapped in double quotation marks("), indicating that it is a valid JSON string rather than a normal string. In JSON, string values must be wrapped in double quotation marks.
obclient [infotest]> SELECT JSON_SCHEMA_VALID('{"type": "string"}', '"JSON_doc"');
The return result is as follows:
+-------------------------------------------------------+
| JSON_SCHEMA_VALID('{"type": "string"}', '"JSON_doc"') |
+-------------------------------------------------------+
| 1 |
+-------------------------------------------------------+
1 row in set
In this example, because the JSON document is a valid JSON string, which conforms to the requirement of the JSON schema, the function returns 1, indicating that the validation succeeds.