๐ 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.
0 characters
Python classes
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
Paste JSON
Paste a sample object or array.
- 2
Choose the output
Dataclasses for plain Python, Pydantic for validation (FastAPI), or TypedDict for type hints on plain dicts.
- 3
Name the class
Change Root to a meaningful name.
- 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.
More from JSON Tools
Free tools that run in your browser โ no signup, nothing uploaded.
JSON Auto-Fixer & Formatter
Repair, validate, and beautify broken JSON (missing quotes, trailing commas).
JSON Validator & Linter
Check JSON syntax and see the exact line and column of every error.
JSON Viewer (Tree)
Explore JSON as a collapsible tree with search and click-to-copy paths.
JSON Editor
Edit JSON with live validation, a tree view, formatting and key sorting.
JSON Beautifier
Pretty-print and indent JSON for easy reading and debugging.
JSON Compare & Diff Tool
Find the difference between two JSON objects visually and quickly.
JSON Sorter (Sort Keys)
Sort JSON object keys alphabetically, including nested objects.
JSONPath Tester
Evaluate JSONPath expressions with filters and recursive queries.
JSON Schema Generator
Generate a JSON Schema from any sample JSON document.
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.
