About the JSON to Python Dataclass Generator
Python lets you work with JSON as dicts, but typed classes give you attribute access, editor completion and a place to hang validation. This generator emits a @dataclass for every object shape in the sample with type hints from the typing module: str, int, float, bool, List[T] and Optional[Any] for nulls.
Classes are written in dependency order, leaf classes first, so the file runs without forward references. Field names are kept as in the JSON with invalid characters replaced by underscores; if your keys are camelCase you may prefer to rename them to snake_case and map them in a loader.
The output is intentionally library-neutral. To use Pydantic, change @dataclass to a BaseModel subclass; to use attrs, swap the decorator. Building the objects from a dict still needs a small loader or a library such as dacite, because dataclasses do not parse nested dicts on their own.
How to use
- Paste JSON on the left, then type the name you want for the top-level dataclass.
- Copy the dataclasses into a
models.pyfile. - Load data with
Root(**json.loads(text))for flat objects, or a nested loader for deeper documents.
Common questions
- Can it generate Pydantic models instead?
- Change the decorator to a
BaseModelbase class and remove@dataclass; the type hints are the same. - Why is a field typed Optional[Any]?
- It was null in the sample, so its real type is unknown. Replace with the correct type.
- Which Python version is required?
- Python 3.7 or later for dataclasses. The typing imports work on all supported versions.