About the JSON to Python
Turn a JSON sample into Python models: dataclasses with a from_dict() constructor, Pydantic v2 models with field aliases, or TypedDict definitions. Names are converted to snake_case and Python keywords are handled safely.
Key features
- dataclasses with a from_dict() classmethod
- Pydantic v2 models with Field aliases
- TypedDict definitions with NotRequired for optional keys
- snake_case attribute names; Python keywords made safe
- Optional[...] for nullable values
- Classes ordered so every reference is defined first
Python models from JSON in one click
Working with JSON as nested dictionaries means typos, KeyErrors and no autocomplete. Generate typed Python models instead: dataclasses for zero-dependency code, Pydantic v2 for validation, or TypedDicts to keep using dicts with type hints.
Example
{ "userId": 7, "class": "admin", "address": { "city": "London" }, "manager": null }
dataclass (default):
@dataclass
class Root:
user_id: int
class_: str
address: Address
manager: Any = None
@classmethod
def from_dict(cls, d: dict[str, Any]) -> Root:
return cls(
user_id=d["userId"],
class_=d["class"],
address=Address.from_dict(d["address"]),
manager=d["manager"],
)
Pydantic v2:
class Root(BaseModel):
model_config = ConfigDict(populate_by_name=True)
user_id: int = Field(alias="userId")
class_: str = Field(alias="class")
address: Address
manager: Any # present but null in the sample, so still required
root = Root.model_validate(data)
Choosing a style
| dataclass | Pydantic v2 | TypedDict | |
|---|---|---|---|
| Dependencies | None | pydantic | None |
| Runtime validation | ❌ | ✅ | ❌ |
| Works with plain dicts | ❌ | ❌ | ✅ |
| Nested parsing | from_dict() | automatic | n/a |
| Best for | Scripts, internal models | APIs, FastAPI, settings | Type-hinting existing dict code |
Tips
- Provide several array items so optional keys get
Nonedefaults. - Use with FastAPI: Pydantic models work directly as request and response models.
- Type-check with mypy or pyright for the most value.
Related tools
Generating for other back ends? See JSON to Go, JSON to C# or JSON to Java.
How to use the JSON to Python
- Paste your data into the input box, upload a file, or click Sample to try it out.
- Adjust the options above the editor if you need to (indentation, delimiter, and so on).
- The Python appears instantly in the output panel. Any errors are shown with their line and column.
- Click Copy to copy the result, or Download to save it as a file.