Turn a JSON sample into Python dataclasses, Pydantic v2 models or TypedDicts, with snake_case names, aliases and Optional fields
Paste an API response and get Python classes for it: a dataclass, a Pydantic model that validates, or a TypedDict for type checkers.
How to use it
Paste a JSON sample, or open a .json file. Choose the style: dataclass, Pydantic or TypedDict, and set the root class’s name. Copy the classes or download models.py. Nested classes come first, so the file runs as it is.
Examples
A Pydantic model with an alias
{"orderId": "A-17", "total": 42.5, "lines": [{"sku": "MUG-01", "qty": 2}]}from pydantic import BaseModel, Field
class Line(BaseModel):
sku: str
qty: int
class Root(BaseModel):
order_id: str = Field(alias="orderId")
total: float
lines: list[Line]Pydantic names the field order_id and reads "orderId" from the JSON. Root.model_validate(data) checks it.
A key that is a Python keyword
{"name": "Ada", "email": null, "from": "uk"}from dataclasses import dataclass
from typing import Any
@dataclass
class Root:
name: str
email: Any
from_: str # "from" in the JSONfrom can’t be a field name, so it is from_ with a comment naming the key.
The same as a TypedDict
{"name": "Ada", "email": null, "from": "uk"}from typing import Any, TypedDict
Root = TypedDict(
"Root",
{
"name": str,
"email": Any,
"from": str,
},
)A TypedDict keeps the JSON’s keys; one that isn’t a valid name uses this form.
How the types are read
Every object gets its own named type; the name comes from its key (customer → Customer, line_items → LineItem). The objects in one array are merged into one type, so a field missing from some of them is marked optional. A field that is null in one place and a string in another is a nullable string. A field that is only ever null, or mixes kinds such as 1 and "a", gets the language’s “anything” type. Integers and decimals are told apart by how they are written: 25 is an integer, 25.0 and 12.5 are not. An integer past 2,147,483,647 gets a 64-bit type, and one past 64 bits a big-number type, read from the digits, never rounded. Empty arrays get a list of “anything”, since the sample doesn’t say what they hold.
Which style to choose
dataclass: plain classes from the standard library. Field names keep the JSON’s keys where Python allows them, so Root(**data) works for flat objects; nested objects stay dicts unless you convert them. Pydantic (v2): models that validate and convert nested objects for you. Fields get snake_case names with Field(alias=…) for the JSON key. TypedDict: no class at runtime, just a type for mypy or Pyright over the dict json.loads gives you. Optional keys are NotRequired.
Optional and nullable fields
Questions
- Does Pydantic write the JSON keys back?
It reads them by alias. To write them back the same way, call model_dump(by_alias=True) or model_dump_json(by_alias=True). - Why are the classes in reverse order?
A class has to be defined before another one uses it in Python, so nested classes come first and the root class last. - Is my JSON uploaded?
No. Everything runs in your browser, so your JSON stays on your device. Nothing you paste is uploaded unless you share a result, which is locked with your password first.