Fast JSON Schema Validator for Python
A high-performance JSON schema validator for Python, currently at version 2.21.2, with a release cadence of approximately one release per year.
Warnings
- breaking In version 2.21.2, the 'compile' function was moved from 'fastjsonschema.schema' to 'fastjsonschema'.
- gotcha Ensure that the 'jsonschema' package is installed, as 'fastjsonschema' depends on it for schema validation.
Install
-
pip install fastjsonschema
Imports
- compile
from fastjsonschema import compile
Quickstart
import os
import fastjsonschema
# Define your JSON schema
schema = {
'type': 'object',
'properties': {
'name': {'type': 'string'},
'age': {'type': 'integer'}
},
'required': ['name', 'age']
}
# Compile the schema
validate = fastjsonschema.compile(schema)
# Validate data
data = {'name': 'John Doe', 'age': 30}
try:
validate(data)
print('Data is valid')
except fastjsonschema.exceptions.JsonSchemaException as e:
print(f'Validation error: {e.message}')