Menu

MariaDB JSON_SCHEMA_VALID(): Validate JSON Against a Schema

MariaDB JSON_SCHEMA_VALID() checks whether a JSON document conforms to a supplied JSON Schema. It returns 1 when valid and 0 when the document does not conform. The function is available from MariaDB 11.1 and supports JSON Schema Draft 2020 with some exceptions. See the official MariaDB JSON_SCHEMA_VALID reference.

MySQL has a function with the same name, but it uses Draft 4 and does not support external schema resources or $ref. Check the MySQL JSON_SCHEMA_VALID() guide before moving schemas between the two engines.

For a side-by-side comparison of schema dialects, minimum versions, and failure reports, see JSON Schema validation in MySQL and MariaDB.

JSON_VALID() only checks whether input is syntactically valid JSON. Use JSON_SCHEMA_VALID() when values must also follow a required structure or data constraints.

Try a JSON Schema in your browser

This local checker uses a browser-side Draft 2020-12 validator. The validator engine loads only when you click Validate, and neither the schema nor JSON document is uploaded. Keep referenced schemas in the same document under $defs; this checker does not fetch remote $ref resources. format values such as date and email are treated as annotations here, matching MariaDB. The result is not a substitute for running JSON_SCHEMA_VALID() on the target MariaDB server: MariaDB also does not support external schema resources or Hyper-schema keywords.

The browser parses the JSON document before checking it, so duplicate object keys are not preserved: JavaScript keeps the last value. MariaDB documents that JSON functions such as JSON_EXTRACT() expose the first value for a duplicate key. Avoid duplicate keys when using this checker to approximate server-side behavior; see the MariaDB JSON data type notes.

Check a JSON document against a schema

Shortcut: Ctrl+Enter or ⌘+Enter. Use local $defs references; remote schemas are not fetched, and format is not checked.

Syntax

JSON_SCHEMA_VALID(schema, json_doc)

Validate an object

This schema requires an object with an integer id property:

SET @schema = '{
  "type": "object",
  "properties": {"id": {"type": "integer"}},
  "required": ["id"]
}';

SELECT
    JSON_SCHEMA_VALID(@schema, '{"id": 7}') AS valid_document,
    JSON_SCHEMA_VALID(@schema, '{"id": "7"}') AS invalid_document;
+----------------+------------------+
| valid_document | invalid_document |
+----------------+------------------+
|              1 |                0 |
+----------------+------------------+

To enforce a schema when values are written, use the function in a CHECK constraint:

CREATE TABLE events (
    id INT PRIMARY KEY,
    payload JSON CHECK (
        JSON_SCHEMA_VALID(
            '{"type":"object","properties":{"id":{"type":"integer"}},"required":["id"]}',
            payload
        )
    )
);

Supported JSON Schema features

MariaDB documents support for JSON Schema Draft 2020 with these limitations:

  • External schema resources are not supported.
  • Hyper-schema keywords are not supported.
  • format values such as date and email are treated as annotations, not validated.

The result is a Boolean-style 1 or 0; it does not report which schema keyword failed. For JSON syntax validation without schema rules, see JSON_VALID().

Advertisement