jsonschema
jsonschema is the most complete and spec-compliant JSON Schema validator for Python, supporting Draft 3, 4, 6, 7, 2019-09, and 2020-12. The current stable release is 4.26.0 (requires Python ≥ 3.10). Releases follow semantic versioning and ship frequently via GitHub; minor releases are expected to be backwards-compatible while major versions may carry deprecations that become removals.
Warnings
- breaking jsonschema.RefResolver is deprecated since v4.18.0 and will be removed in a future major release. The resolver= constructor kwarg is also deprecated.
- gotcha format keywords (e.g. 'email', 'date-time', 'uri') are NEVER validated unless you explicitly pass format_checker=FormatChecker(). Without it, format is purely informational and invalid values silently pass.
- deprecated Subclassing validator classes (Draft7Validator, Draft202012Validator, etc.) has been explicitly warned against since v4.12.0 and is not public API.
- gotcha validate() raises only the FIRST ValidationError encountered and stops. All subsequent errors in the same instance are silently dropped.
- breaking Python 3.8 support was dropped in v4.24.0. Pinning to <4.24.0 is required to maintain 3.8 compatibility.
- gotcha jsonschema does not fill in 'default' values from the schema into validated instances. The 'default' keyword is purely decorative metadata during validation.
- deprecated ErrorTree.__setitem__ (mutating an ErrorTree after construction) is deprecated since v4.20.0.
Install
-
pip install jsonschema -
pip install "jsonschema[format-nongpl]" -
pip install "jsonschema[format]"
Imports
- validate
from jsonschema import validate
- ValidationError
from jsonschema import ValidationError
- SchemaError
from jsonschema import SchemaError
- Draft202012Validator
from jsonschema import Draft202012Validator
- Draft7Validator
from jsonschema import Draft7Validator
- FormatChecker
from jsonschema import FormatChecker
- RefResolver
from referencing import Registry from referencing.jsonschema import DRAFT202012
- ErrorTree
from jsonschema.exceptions import ErrorTree
Quickstart
from jsonschema import Draft202012Validator, FormatChecker, ValidationError
schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": {"type": "string"},
"price": {"type": "number", "minimum": 0},
"email": {"type": "string", "format": "email"},
},
"required": ["name", "price"],
}
# Reuse the validator object for efficiency (avoid re-creating per call)
# Pass format_checker to activate 'format' keyword assertions;
# without it, 'format' is purely informational and never raises.
validator = Draft202012Validator(schema, format_checker=FormatChecker())
good = {"name": "Widget", "price": 9.99, "email": "user@example.com"}
bad = {"name": "Widget", "price": "free", "email": "not-an-email"}
# validate() raises on the FIRST error; use iter_errors() for all errors.
try:
validator.validate(good)
print("good instance: valid")
except ValidationError as exc:
print(f"Unexpected error: {exc.message}")
all_errors = list(validator.iter_errors(bad))
for err in all_errors:
# err.path holds the JSON path to the failing field
field = " -> ".join(str(p) for p in err.absolute_path) or "<root>"
print(f"[{field}] {err.message}")
# Schema validity check (raises SchemaError if the schema is malformed)
Draft202012Validator.check_schema(schema)