๐Ÿ”’ Runs in your browser โ€” nothing is uploadedโœ“ Free, no signup

JSON to Python Class Generator

Paste JSON to generate Python dataclasses, Pydantic models or TypedDicts with type hints. Nested objects become their own classes, defined before they're used.

Loading...

0 characters

Python classes

Loading...

When should you use the JSON to Python?

  • Typing an API response in a FastAPI or Django project
  • Creating Pydantic models to validate incoming JSON
  • Adding type hints to code that works with JSON data

How to use the JSON to Python

  1. 1

    Paste JSON

    Paste a sample object or array.

  2. 2

    Choose the output

    Dataclasses for plain Python, Pydantic for validation (FastAPI), or TypedDict for type hints on plain dicts.

  3. 3

    Name the class

    Change Root to a meaningful name.

  4. 4

    Copy

    Copy the code or download models.py.

Examples

Pydantic with an optional field

JSON

{
  "orders": [
    { "total": 9.5 },
    { "total": 20, "coupon": "SAVE" }
  ]
}

Python

class Order(BaseModel):
    total: float
    coupon: str | None = None


class Root(BaseModel):
    orders: list[Order]

Common errors and how to fix them

A key was renamed
Keys that aren't valid Python names (like zip-code, class or 2fa) are renamed. Pydantic output keeps the original key with Field(alias=โ€ฆ).
SyntaxError on str | None
The X | None syntax needs Python 3.10+. On older versions, use Optional[X] from typing.
Related tools:ย JSON to TypeScript ยท JSON to Go ยท JSON Schema Generator

More from JSON Tools

Free tools that run in your browser โ€” no signup, nothing uploaded.

JSON to Python FAQ

Use Pydantic when you need to validate or parse untrusted JSON (for example in FastAPI). Use dataclasses for lightweight typed objects, and TypedDict when you keep working with plain dicts.

Keys missing from some items become X | None with a default of None. Dataclass fields with defaults are placed after required fields so the class is valid.

The output uses modern type hints (list[str], X | None), which need Python 3.10 or newer. TypedDict output uses NotRequired, from Python 3.11.

They're converted to valid snake_case names โ€” zip-code becomes zip_code and class becomes class_. Pydantic output adds an alias so the original key still works.

Each nested object becomes its own class, defined before the class that uses it.

No. Everything runs in your browser, so your JSON never leaves your device.