JSON to Python dataclass
Paste a JSON sample to get Python classes: @dataclass or Pydantic BaseModel, typing hints (Optional, list) and snake_case field names, originals noted.
How to generate Python classes from JSON
- Paste a JSON sample on the left or open a .json file; the Python code appears on the right.
- Choose @dataclass or Pydantic, and rename the root type if you like.
- Copy it or download the .py file.
Frequently asked questions
@dataclass is built into Python and suits plain data holders. Pydantic (installed separately) checks and converts values while reading JSON, and is widely used with FastAPI.
Python uses snake_case, so firstName becomes first_name, and keywords like class become class_. With @dataclass the original name is noted at the end of the line; with Pydantic an alias keeps reading the original JSON.
In a @dataclass, fields with a default (= None, used for fields that are not always there) must come after fields without one, so they are moved down.
Python 3.9 or later (the code uses list[str]). The Pydantic code is written for Pydantic v2.
It is completely free and needs no sign-up.
Related tools
JSON to TypeScriptGenerate TypeScript interfaces from sample JSONJSON to Go structGenerate Go structs from a JSON sampleJSON to Java classGenerate Java classes or records from JSONJSON to Kotlin data classGenerate Kotlin data classes from a JSON sampleJSON Escape / UnescapeTurn text into a valid JSON string and backJSON FormatterFormat, minify and validate JSONJSON RepairAutomatically fix broken JSONJSON CompareFind the differences between two JSON documentsJSON ViewerView JSON as a tree, collapse or expand any branch