Simplify data validation by converting JSON Schema to Python dataclasses
S

Simplify data validation by converting JSON Schema to Python dataclasses

Simplify data validation by converting JSON Schema to Python dataclasses

JavaScript
3,952 stars
N/A forks
N/A contributors

README

Project documentation from GitHub

From JSON Schema to Python Dataclasses: A Cleaner Way to Validate Data

If you've ever built an API, processed configuration files, or handled any kind of structured data in Python, you've probably felt the pain. You write a JSON Schema to define the exact shape your data should take, and then you write a separate set of Pydantic models or dataclasses in your actual code to work with that data. Keeping these two definitions in sync is a recipe for subtle bugs and maintenance headaches.

What if you could just write your schema once and automatically get the Python classes? That's exactly what datamodel-code-generator does. It takes your JSON Schema, OpenAPI specs, or even GraphQL introspection queries and turns them directly into ready-to-use Python dataclasses or Pydantic models.

What It Does

datamodel-code-generator is a command-line tool and library that acts as a bridge between data definitions and your runtime code. You feed it a schema file, and it outputs a clean Python file containing dataclasses or Pydantic BaseModel classes that mirror that schema's structure, complete with type hints.

It handles the tedious translation: required properties, nested objects, enums, and complex constraints get converted into proper Python constructs. The generated code is immediately usable for validation with Pydantic or for type-safe data handling with standard dataclasses.

Why It's Cool

The magic here is in the automation and the choices the tool makes for you. It's not just a simple one-to-one mapping; it intelligently resolves references ($ref), handles unions and anyOf/oneOf schemas, and can even generate forward references for recursive structures. This means the code it produces is practical, not just theoretically correct.

It supports multiple input formats (JSON Schema, OpenAPI, GraphQL) and multiple output targets (Pydantic v1/v2, standard dataclasses, or even msgspec structs). This flexibility makes it a perfect fit for modern Python stacks. You can use it to instantly generate client libraries from an OpenAPI spec, or to create your application's core data models from a central schema definition, ensuring your types are always correct.

How to Try It

Getting started is straightforward. Install the package via pip:

pip install datamodel-code-generator

Then, point it at your schema file. For example, if you have a config_schema.json:

datamodel-code-generator --input config_schema.json --output models.py --input-file-type jsonschema

Open the generated models.py file, and you'll find your dataclasses ready to import and use. The project's GitH

Did you like this issue?

Join our weekly newsletter

Related Projects

Love discovering amazing projects?

Help us continue bringing you the best open-source discoveries every week.

Back to Projects
Last updated: Dec 29, 2025