Purpose
This function is used to validate a JSON document. It returns 1 if the JSON document conforms to the JSON Schema specification, and 0 otherwise. It can be used in column constraints.
Syntax
JSON_SCHEMA_VALID(schema, document)
Purpose
The
schemaparameter specifies the JSON Schema, which must be a valid JSON object.The
requiredattribute is supported in JSON Schema to enforce the inclusion of specific properties.The
id,$schema,description, andtypeattributes are supported in JSON Schema, but they are not required.
The
documentparameter specifies the JSON document to be validated, which must be a valid JSON document.If either parameter is not a valid JSON, the function will raise an error.
Examples
In the following example, a JSON Schema is defined using '{"type": "string"}', indicating that valid JSON data should be of the string type. The '"JSON_doc"' is the JSON document to be validated.
Notice
In this statement, the string JSON_doc is enclosed in double quotes ("), indicating it is a valid JSON string, not just a regular string. In JSON, string values must be enclosed in double quotes.
obclient [infotest]> SELECT JSON_SCHEMA_VALID('{"type": "string"}', '"JSON_doc"');
The result is as follows:
+-------------------------------------------------------+
| JSON_SCHEMA_VALID('{"type": "string"}', '"JSON_doc"') |
+-------------------------------------------------------+
| 1 |
+-------------------------------------------------------+
1 row in set
In this example, since the JSON document is a valid JSON string that conforms to the string type specified in the schema, the function returns 1, indicating the validation was successful.
