JSON to Python Dataclass
Infer Python dataclasses from JSON.
About this tool
Paste a JSON object and get a Python dataclass with type hints on every field. Lists become List[T] and nested objects become dict[str, Any]. It also writes the dataclass and typing imports.
How to use
How it works
Each key becomes a field annotated with str, int, float, bool, List[T] (items merged, mixed types become Union) or dict[str, Any]. The output starts with the dataclasses and typing imports and a @dataclass decorator on a class named Root.
Worked example
A scored item
- Input
- {"id":1,"name":"Ada","score":9.5,"tags":["a"],"active":true}
- Output
- from dataclasses import dataclass from typing import Any, List, Union @dataclass class Root: id: int name: str score: float tags: List[str] active: bool
- Walkthrough
- Union is imported for mixed-type arrays; delete the import if you do not need it.
Limitations
- Nested objects are dict[str, Any], not nested dataclasses.
- Keys that are not valid Python identifiers (such as user-id) are not renamed.
- No default values, Optional fields or from_dict method are generated.
Questions
How do I generate a Python dataclass from JSON?
Does it create nested dataclasses?
Which Python versions does the output need?
Can it load JSON into the dataclass?
Features
- Type hints on every field
- Imports included
- List[T] from arrays
- Runs in your browser
Related resources
Guides and videos from around the web—opens on YouTube or the publisher’s site.
What Is A RESTful API?
Traversy Media
Most APIs exchange JSON—context for pretty-print and schema checks.
Watch on YouTubeInspect Network Activity - Chrome DevTools 101
Chrome for Developers
Debugging payloads you paste into JSON and JWT tools.
Watch on YouTube
- Getting started with JSON Schema
JSON Schema
When you outgrow ad-hoc validation and need a schema definition.
- RFC 8259 — The JavaScript Object Notation
IETF
The standards text behind modern JSON interoperability rules.