jsonschema

raw JSON →
4.26.0 verified Tue May 12 auth: no python install: verified quickstart: verified

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.

pip install jsonschema
error 'property_name' is a required property
cause This `ValidationError` occurs when the JSON instance being validated is missing a property that is marked as `required` in the JSON Schema.
fix
Ensure that the JSON instance includes all properties listed in the required array of your schema. For example, if your schema has "required": ["name"], your instance must contain a "name" field.
error jsonschema.exceptions.SchemaError: 'keyword' is not a valid JSON Schema keyword
cause This `SchemaError` indicates that the JSON Schema itself is invalid, often due to a typo in a keyword or using a keyword that is not recognized by the JSON Schema draft being used.
fix
Correct the misspelled keyword or ensure that all keywords used are valid for the JSON Schema draft specified (or implicitly used if $schema is omitted). Validate your schema against a linter or the official specification.
error ImportError: No module named 'jsonschema.compat'
cause This error typically arises when code written for `jsonschema` version 3.x is run with `jsonschema` version 4.x or newer, as the `jsonschema.compat` module was removed in version 4.0.
fix
Either update your code to remove reliance on the deprecated compat module and use equivalent functionalities available in jsonschema v4+, or downgrade jsonschema to a version less than 4.0 using pip install 'jsonschema<4.0'.
error ImportError: cannot import name 'validate' from 'jsonschema'
cause This `ImportError` usually occurs because the `validate` function's direct import path changed in newer versions of the `jsonschema` library, or it's meant to be called differently.
fix
Instead of from jsonschema import validate, you should import it from the jsonschema module directly if using jsonschema.validate (e.g., import jsonschema then jsonschema.validate(instance, schema)). In older versions where validate was a top-level function, ensure your environment's jsonschema version matches the expected API. If you are using a Validator class, you would call validator.validate(instance) instead.
error jsonschema.exceptions.RefResolutionError: Unresolvable JSON pointer: 'some/path'
cause This error signifies that a `$ref` keyword in your JSON Schema could not be resolved. This often happens if the path in the `$ref` is incorrect, the referenced definition doesn't exist, or there are issues with resolving local or remote URIs.
fix
Verify the $ref path to ensure it correctly points to an existing definition within the current schema (e.g., #/definitions/myDefinition) or a valid external schema. Ensure any external schemas are accessible and correctly specified. Consider using RefResolver for complex reference handling if necessary.
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.
fix Replace RefResolver with a referencing.Registry object and pass it as the registry= kwarg: `from referencing import Registry; from referencing.jsonschema import DRAFT202012; registry = Registry().with_resource('http://example.com', DRAFT202012.create_resource({...})); Draft202012Validator(schema, registry=registry)`.
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.
fix Pass format_checker=FormatChecker() to validate() or the validator constructor. Also install the [format-nongpl] or [format] extras so the underlying checker packages (e.g. email-validator, rfc3339-validator) are present.
deprecated Subclassing validator classes (Draft7Validator, Draft202012Validator, etc.) has been explicitly warned against since v4.12.0 and is not public API.
fix Use jsonschema.validators.extend() or jsonschema.validators.create() to produce custom validator classes instead of inheriting directly.
gotcha validate() raises only the FIRST ValidationError encountered and stops. All subsequent errors in the same instance are silently dropped.
fix Use Validator.iter_errors(instance) to lazily yield every error, or Validator.is_valid(instance) for a simple boolean check without exceptions.
breaking Python 3.8 support was dropped in v4.24.0. Pinning to <4.24.0 is required to maintain 3.8 compatibility.
fix Upgrade to Python 3.10+ (the current requires-python floor). If you must stay on 3.8, pin jsonschema<4.24.0.
gotcha jsonschema does not fill in 'default' values from the schema into validated instances. The 'default' keyword is purely decorative metadata during validation.
fix If you need default-filling behavior, write a custom validator using jsonschema.validators.extend() that handles the 'default' keyword, or use a separate post-validation step.
deprecated ErrorTree.__setitem__ (mutating an ErrorTree after construction) is deprecated since v4.20.0.
fix Pass all sub-errors at construction time: ErrorTree(errors=list_of_errors). Do not mutate the tree after creation.
pip install "jsonschema[format-nongpl]"
pip install "jsonschema[format]"
python os / libc variant status wheel install import disk
3.10 alpine (musl) format-nongpl - - 5.55s 28.1M
3.10 alpine (musl) format - - 0.33s 27.2M
3.10 alpine (musl) jsonschema - - 0.20s 21.5M
3.10 slim (glibc) format-nongpl - - 3.98s 28M
3.10 slim (glibc) format - - 0.23s 27M
3.10 slim (glibc) jsonschema - - 0.14s 22M
3.11 alpine (musl) format-nongpl - - 6.99s 30.7M
3.11 alpine (musl) format - - 0.48s 29.6M
3.11 alpine (musl) jsonschema - - 0.34s 23.7M
3.11 slim (glibc) format-nongpl - - 5.37s 31M
3.11 slim (glibc) format - - 0.40s 30M
3.11 slim (glibc) jsonschema - - 0.26s 24M
3.12 alpine (musl) format-nongpl - - 5.96s 22.4M
3.12 alpine (musl) format - - 0.40s 21.3M
3.12 alpine (musl) jsonschema - - 0.26s 15.5M
3.12 slim (glibc) format-nongpl - - 6.29s 22M
3.12 slim (glibc) format - - 0.40s 21M
3.12 slim (glibc) jsonschema - - 0.35s 16M
3.13 alpine (musl) format-nongpl - - 6.01s 21.7M
3.13 alpine (musl) format - - 0.38s 20.6M
3.13 alpine (musl) jsonschema - - 0.26s 14.8M
3.13 slim (glibc) format-nongpl - - 5.69s 22M
3.13 slim (glibc) format - - 0.38s 21M
3.13 slim (glibc) jsonschema - - 0.25s 15M
3.9 alpine (musl) format-nongpl - - 5.68s 27.5M
3.9 alpine (musl) format - - 0.35s 26.6M
3.9 alpine (musl) jsonschema - - 0.26s 21.0M
3.9 slim (glibc) format-nongpl - - 4.72s 28M
3.9 slim (glibc) format - - 0.32s 27M
3.9 slim (glibc) jsonschema - - 0.23s 21M

Validate a dict against a JSON Schema, collect all errors with a versioned validator, and demonstrate format checking.

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)