JSON to Python Dataclass
Generate Python dataclasses from JSON instantly. Supports @dataclass, Pydantic v2 BaseModel, and TypedDict. Handles nested objects, arrays, optional fields, and custom root class names. Free, runs in your browser.
How to Use
- Paste or type your JSON into the left panel. The tool validates it in real time.
- Optionally set a Root class name (default:
Root). - Choose an output mode: @dataclass, Pydantic v2, or TypedDict.
- Click Generate Python or wait for real-time conversion.
- Click Copy to copy the output, or Download .py to save as a file.
Type Mapping
null→Optional[Any] = None- string →
str - integer →
int - float →
float - boolean →
bool - array →
List[T] - mixed array →
List[Union[T1, T2]] - nested object → separate class
Example
Given this JSON:
{"user": {"first_name": "Alice", "age": 30}, "tags": ["admin"], "note": null}
The tool generates (@dataclass mode):
from dataclasses import dataclass
from typing import Any, List, Optional
@dataclass
class User:
first_name: str
age: int
@dataclass
class Root:
user: User
tags: List[str]
note: Optional[Any] = None FAQ
What does this tool generate?
It generates Python class definitions from JSON. Choose between standard @dataclass (stdlib), Pydantic v2 BaseModel, or TypedDict. Each nested object becomes its own named class.
What output modes are supported?
Three modes: @dataclass (Python stdlib, default), Pydantic v2 BaseModel, and TypedDict (Python 3.8+).
How are JSON types mapped to Python types?
string → str, integer → int, float → float, boolean → bool, null → Optional[Any], array → List[T], nested object → separate class.
When are fields marked as Optional?
A field is marked Optional[T] = None when its value is null in the JSON sample, or when it is missing in some objects of a merged array.
How are nested objects handled?
Each nested object is extracted into a separate class named after the field key in PascalCase. Child classes are always defined before the parent class so the output is immediately usable.
Is my data sent to a server?
No. The entire conversion runs in your browser. No data leaves your machine.