JSON to Python Class Generator – Dataclass & Pydantic

Paste JSON and get Python classes with type hints: dataclasses, Pydantic models or TypedDict, with nested classes and optional fields.

JSON Input

Ln 1, Col 1Empty

Python

Your result will appear here.
No result yet

What Is a JSON to Python Converter?

A JSON to Python converter reads example JSON and writes Python classes with type hints that match it. Instead of working with untyped dictionaries, you get classes your editor understands, with autocomplete and type checking.

How to Generate Python Classes from JSON

  1. Paste a sample of your JSON, or drop a .json file on the box.
  2. Pick a style: dataclass, Pydantic model or TypedDict.
  3. Click Generate Python, or press Ctrl + Enter.
  4. Copy the code or download models.py.

Which Style Should I Use?

  • dataclass: built into Python, no extra packages. Good for simple data.
  • Pydantic: validates data when you load it. Popular with FastAPI. Keys like firstName are mapped with an alias.
  • TypedDict: keeps your data as normal dictionaries, but adds type hints.

Example

JSON
{
  "user_id": 42,
  "firstName": "Emily",
  "email": "emily@example.com",
  "tags": ["admin"],
  "address": { "city": "London" },
  "manager": null
}
Python (dataclass)
from __future__ import annotations

from dataclasses import dataclass
from typing import Any

@dataclass
class Address:
    city: str


@dataclass
class User:
    user_id: int
    first_name: str  # JSON key: "firstName"
    email: str
    tags: list[str]
    address: Address
    manager: Any | None

firstName becomes the Python-style name first_name, the nested object gets its own Address class, and manager is Any | None because the sample only shows null. Replace Any with the real type if you know it.

Loading JSON into the Classes

Pydantic
user = User.model_validate_json(json_text)   # parses, checks types, uses the aliases
print(user.first_name)

Pydantic converts nested objects and renamed keys for you and raises a clear error when the data does not match. With dataclasses, load the JSON with json.loads first, then build the objects yourself, including the nested ones, because User(**data) does not convert nested dictionaries or renamed keys. More about the json module in JSON in Python.

Frequently Asked Questions

Which Python version do I need?

The code uses modern type hints like list[str] and str | None, which work in Python 3.10 and newer.

How are camelCase keys handled?

Field names are turned into snake_case. With Pydantic, an alias keeps the original JSON key, so loading the JSON still works.

What is the difference between dataclass and Pydantic?

Dataclasses are built into Python and only store data. Pydantic models also check and convert the data when you load it, which is useful for APIs.

Is my JSON uploaded?

No. The classes are generated in your browser.

Related Tools