About the JSON to Python Class tool
Type hints make Python code easier to read and let editors and mypy catch mistakes, but writing classes for a large JSON payload by hand is tedious. This JSON to Python generator turns a sample into ready-to-use class definitions in the style you prefer.
Choose @dataclass for standard-library classes, TypedDict when you keep working with plain dictionaries, or Pydantic BaseModel for runtime validation and parsing. Nested objects become their own classes, declared before the classes that use them. Types are inferred as str, int, float, bool, List[...] and Optional[...], and mixed values become Union. For dataclasses and Pydantic, fields are converted to snake_case; Pydantic fields keep the original key through Field(alias=...), and dataclass fields note the original key in a comment. Python keywords such as class are safely renamed to class_.
Keys that are missing from some items in a JSON array are treated as optional: default None for dataclasses and Pydantic, NotRequired for TypedDict. The conversion happens in your browser.
How to use JSON to Python Class
- Paste JSON into “JSON sample”, open a file, or click “Load sample”.
- Enter the “Root type name” for the top-level class.
- Choose the “Output style”: @dataclass, TypedDict or Pydantic BaseModel.
- For dataclasses, decide whether to use “snake_case field names”.
- Copy the code or download it as a .py file.
Features
Three output styles
Standard dataclasses, typing.TypedDict or Pydantic models from the same JSON.
Aliases for original keys
Pydantic fields keep camelCase or dashed JSON keys with Field(alias=...).
Valid TypedDict for any key
Keys that are not valid identifiers switch to the functional TypedDict syntax.
Correct dataclass ordering
Optional fields with defaults are placed after required ones, as dataclasses require.
Only needed imports
typing imports are generated for exactly the names used.
When to use it
- Create Pydantic models for a FastAPI endpoint from an example payload.
- Add type hints to code that consumes a third-party REST API.
- Describe JSON configuration files with TypedDict for mypy checks.
- Build dataclasses for test fixtures loaded from JSON.
- Document the shape of data passed between services.
Frequently asked questions
Which Python version does the output need?
The code uses from __future__ import annotations and typing names, so it runs on Python 3.8+. TypedDict with NotRequired needs 3.11 or typing_extensions, as noted in the output.
Is it Pydantic v1 or v2?
The BaseModel and Field(alias=...) syntax works in both. In v2, aliased fields are populated by the JSON key when you call model_validate.
Will Root(**data) work with a dataclass?
Only when field names match the JSON keys. Turn off snake_case to keep the original keys, or map keys yourself when they differ.
Why is a field typed Any?
The sample only had null for it. Replace Any with the real type once you know it.
Is my JSON uploaded?
No. Everything is generated locally in your browser.