JSON Schema to Pydantic Model Generator

0.4.11 · active · verified Tue Apr 14

json-schema-to-pydantic is a Python library designed to automatically generate Pydantic v2 models from JSON Schema definitions. It supports complex schema features like references, combiners (allOf, anyOf, oneOf), type constraints, and format validations. The library is actively maintained, with regular updates and improvements, currently at version 0.4.11.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a basic JSON Schema and use `create_model` to generate a Pydantic v2 model. It then shows how to instantiate and use the generated model for data validation and serialization.

from json_schema_to_pydantic import create_model

# Define your JSON Schema
schema = {
    "title": "User",
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "email": {"type": "string", "format": "email"},
        "age": {"type": "integer", "minimum": 0}
    },
    "required": ["name", "email"]
}

# Generate your Pydantic model
UserModel = create_model(schema)

# Use the model
user_data = {"name": "John Doe", "email": "john@example.com", "age": 30}
user = UserModel(**user_data)

print(user.model_dump_json(indent=2))
# Expected output (approx):
# {
#   "name": "John Doe",
#   "email": "john@example.com",
#   "age": 30
# }

view raw JSON →