JSON to Python Dataclass Generator
Paste JSON and instantly generate Python dataclasses with type hints, nested objects, and Optional types
Paste any JSON — flat or deeply nested — and get typed Python @dataclass or TypedDict definitions with correct imports, snake_case fields, Optional handling, and List[T] annotations in one click. Beats writing those classes by hand when you're staring at a 150-key API response. And everything runs in your browser. Nothing gets uploaded.
How It Works
The converter parses your JSON, walks every key-value pair, and maps each value to a Python type — str, int, float, bool, or a nested class. When it hits an object inside another object, it creates a separate class with a PascalCase name pulled from the key. Arrays get typed as List[T] based on the first element. The final output is ordered so child classes come before parents — paste it into a .py file and it just works.
Examples
Simple User Profile
A flat object from a user API endpoint — no nesting, just basic fields.
{
"userName": "jdoe",
"email": "[email protected]",
"age": 28,
"isActive": true
}
@dataclass
class Root:
user_name: str
email: str
age: int
is_active: bool
Nested API Response with Address
When the JSON has an object inside an object, each one becomes its own dataclass. This is where manual typing starts to hurt.
{
"id": 42,
"name": "Acme Corp",
"address": {
"street": "123 Main St",
"city": "Springfield",
"zip": "62704"
}
}
@dataclass
class Address:
street: str
city: str
zip: str
@dataclass
class Root:
id: int
name: str
address: Address
E-commerce Order with Arrays and Nulls
Real-world JSON with arrays of objects, nullable fields, and mixed types. This is the kind of payload that takes 15+ minutes to type manually.
{
"orderId": "ORD-9981",
"total": 129.95,
"coupon": null,
"items": [
{
"sku": "WDG-42",
"qty": 2,
"price": 64.975
}
]
}
@dataclass
class Item:
sku: str
qty: int
price: float
@dataclass
class Root:
order_id: str
total: float
coupon: Optional[Any]
items: List[Item]
Who Uses This
Turn API responses into typed dataclasses for FastAPI, Flask, or Django projects.
Generate classes from JSON config files or pipeline schemas before writing ETL logic.
See how JSON maps to Python types — helpful when learning about type hints and dataclasses.
Paste a sample response from Postman or curl, get typed models instantly for any third-party API.
Common Mistakes
Trailing commas, single quotes instead of double quotes, or unquoted keys will all fail. Run your JSON through a validator first if you're getting parse errors. The tool shows the exact error message from the parser, so check the line number it points to.
If your JSON root is an array, the tool uses the first element to infer the class structure. An array of 500 objects won't create 500 classes — it reads element zero and builds one class from that. Make sure the first item is representative of the rest.
If your JSON has null values but you leave "Wrap nulls in Optional" off, those fields become Any instead of Optional[str] or similar. Turn it on when your data has real nulls — it produces cleaner type hints that mypy and Pyright actually catch errors with.
Technical Details
- Processing: 100% client-side — JavaScript in your browser, no server calls
- Browser Support: Chrome, Firefox, Safari, Edge
- Output Formats: @dataclass (Python 3.7+) or TypedDict (Python 3.8+)
- Name Conversion: camelCase / PascalCase keys → snake_case fields automatically
- Download: One-click
.pyfile export
Frequently Asked Questions
What Python types does the generator produce?
Strings map to str, integers to int, decimals to float, and booleans to bool. Null values become Optional[Any] when you turn on the Optional toggle. Arrays become List[T] where T is inferred from the first element, and nested objects become their own dataclass with a PascalCase name.
How does it handle nested JSON objects?
Each nested object gets its own class. If you have a "billing" key with an object value, the tool creates a Billing dataclass and references it in the parent. Classes are ordered so dependencies come first — you can paste the output directly into a file without reordering anything.
What is the difference between dataclass and TypedDict?
A @dataclass gives you a real class with __init__, __repr__, and __eq__ built in. A TypedDict keeps the dictionary interface but adds type checking on keys. Pick dataclass if you want object-oriented code. Pick TypedDict if your codebase passes dicts around and you just want the type checker to catch mistakes.
Does it convert camelCase keys to snake_case?
Yes, automatically. firstName becomes first_name, orderId becomes order_id. Python reserved words like class or type get an underscore appended — so class becomes class_. You don't need to rename anything after generating.
Is my JSON data safe when I use this tool?
Completely. The conversion runs in your browser with JavaScript — nothing gets sent to a server. Your data stays on your machine the entire time. You can disconnect from the internet and the tool still works, which is the easiest way to verify for yourself.