JSON to Python Dataclass & Pydantic Generator
Turn a JSON sample into typed Python: standard-library dataclasses or Pydantic v2 models, with snake_case field names, aliases for the original keys, and X | None for optional values.
JSON to Python Class
Runs in your browser — nothing you enter is uploaded or stored.
Scratchpad auto-saved
How it works — in four simple steps
No signup and nothing to install — it all runs in your browser.
Add your JSON
Paste it into the left box, press Open file, or drag a .json file onto it. Sample loads a realistic example.
Set the options
Adjust the options above the boxes — the output updates instantly as you change them.
Check the result
Read the status line: it reports rows, keys and any value that couldn't be represented in Python.
Copy or download
Copy the Python or download it as a .py file. Nothing was uploaded at any point.
Pydantic mode keeps the original keys as aliases, so Model.model_validate(raw_json) just works.
Features
Named types
Choose the root type name; nested objects become their own named types, and identical shapes are merged.
dataclass or Pydantic
Standard-library @dataclass, or Pydantic v2 models with Field(alias=…) for keys that aren't valid Python names.
Private by design
Parsing and conversion run in your browser with JavaScript — your data is never uploaded, logged or stored.
About the JSON to Python Class
Typed models make JSON-handling code safer and easier to autocomplete. This generator infers a class for every object in your JSON — the root, each nested object, and the items of each array (singularised: books → Book) — and writes them dependency-first, so every class is defined before it's used.
Field names are converted to PEP 8 snake_case (inStock → in_stock); keywords like class or from get a trailing underscore. In Pydantic mode the original key is kept with Field(alias="inStock"), so Model.model_validate(data) works on the raw JSON. In dataclass mode, renamed keys are noted in a comment, since dataclasses don't do aliasing. Types follow modern syntax: list[str], dict[str, Any] and int | None.
Keys missing from some objects, or sometimes null, become X | None = None and are placed after the required fields, as dataclasses require. The file starts with from __future__ import annotations, so the | syntax works on Python 3.7+, and only the imports actually used are included.
Examples
A book record with camelCase keys, a nested publisher, a list of authors and an optional field.
{
"isbn": "978-0131103627",
"title": "The C Programming Language",
"inStock": true,
"price": 45.5,
"publisher": { "name": "Prentice Hall", "country": "US" },
"authors": [ { "firstName": "Brian", "lastName": "Kernighan" }, { "firstName": "Dennis", "lastName": "Ritchie", "died": 2011 } ]
}from __future__ import annotations
from dataclasses import dataclass
@dataclass
class Author:
first_name: str # JSON key: "firstName"
last_name: str # JSON key: "lastName"
died: int | None = None
@dataclass
class Publisher:
name: str
country: str
@dataclass
class Book:
isbn: str
title: str
in_stock: bool # JSON key: "inStock"
price: float
publisher: Publisher
authors: list[Author]This output was produced by the converter on this page with root name Book (switch Style to Pydantic for Field aliases) — load it with the “Book API” preset above and change any option to see the effect.
How JSON maps to Python
| In JSON | Becomes in Python |
|---|---|
"name": "Ada" | name: str |
36 / 36.5 | int / float |
| Nested object | its own class |
["a"] | list[str] |
| Missing or null in some items | x: int | None = None |
Key inStock | in_stock + Field(alias="inStock") (Pydantic) |
Key class | class_ |
Things that trip people up
- Dataclasses don't parse nested dicts.
Root(**data)leaves nested values as plain dicts. Use Pydantic mode, or a library like dacite, to build nested objects automatically. - Pydantic aliases. With aliases, construct from JSON using the original keys. To also allow field names, set
model_config = ConfigDict(populate_by_name=True). - Dates stay str. Change ISO date fields to
datetimeordate; Pydantic parses ISO 8601 strings into them. - Python version.
list[str]at runtime needs 3.9+ andX | Noneneeds 3.10+; with the future import at the top they're fine in annotations from 3.7.
JSON vs Python at a glance
| Property | JSON | Python |
|---|---|---|
| Specification | RFC 8259 (2017) · ECMA-404 | dataclasses (3.7+) · Pydantic v2 |
| File extension | .json | .py |
| Media type | application/json | text/x-python |
| Data types | string, number, boolean, null, object, array | str, int, float, bool, list, None |
| Comments | No | Yes |
| Nesting | Unlimited | Named classes |
- JSON: Numbers are parsed as IEEE-754 doubles in JavaScript, so integers above 9,007,199,254,740,991 (2⁵³ − 1) lose precision.