Code Beautifier

JSON to Python (Pydantic)

Generate Pydantic v2 models, dataclasses, or TypedDicts from JSON, with snake_case fields, aliases for the original keys, and Optional where data can be missing.

Draft saved locally.

Local workspace

Named projects in IndexedDB · Local only — never synced to our servers. Worksp

Open manager

Batch workspace

Format multiple files locally in one run.

Style:
json
Pythonpython

Python code that consumes JSON usually starts with dictionaries and ends up with KeyError and TypeError far from where the data arrived. A Pydantic model validates the payload at the boundary and gives you attributes your editor can complete. This generator merges every array item to find optional and nullable fields, converts keys like first-name and class to valid snake_case names with aliases, orders dataclass fields so the class definition is valid, and switches to the functional TypedDict syntax when keys can't be identifiers.

Common errors and fixes

Input should be a valid integer

Pydantic v2's error when a value like "abc" or 18.25 reaches an int field. Generate from a sample with the real type, or change the annotation to float or str.

Field required

A key is missing from the data. Include a sample without it so the field is generated as Optional[...] = None.

TypeError: non-default argument follows default argument

A dataclass field without a default comes after one with a default. Keep required fields first, as the generator writes them.

Field name "json" shadows an attribute in parent "BaseModel"

Keys like json or schema clash with BaseModel methods. The generator renames them with a trailing underscore and keeps the key as an alias.

Options

OptionDescription
StylePydantic validates data at runtime. A dataclass is a plain container. A TypedDict only describes dictionaries for type checkers.
Root nameName of the top-level class. Nested classes are named after their JSON keys.

FAQ

Should I use Pydantic, a dataclass, or a TypedDict?

Use Pydantic when data comes from outside your program, such as an API, because it validates and converts values at runtime. A dataclass is a lightweight container with no validation. A TypedDict keeps the data as a dict and only helps type checkers like mypy.

Why do some fields use Field(alias=...)?

Python attribute names must be identifiers, and PEP 8 prefers snake_case, so first-name becomes first_name and class becomes class_. The alias maps each one back to the JSON key, and populate_by_name lets your code use either name.

Why does a field become Optional[int] = None?

The key is missing from some samples or holds null. The default of None makes Pydantic treat the field as not required, so data without it still validates.

Which Python version does the output need?

Built-in generics such as list[str] need Python 3.9 or later, and NotRequired in TypedDict output needs 3.11 or typing_extensions. The Pydantic style targets Pydantic v2.

Why are dataclass fields reordered?

Dataclass fields with a default must come after fields without one, or Python raises a TypeError when the class is defined. Required fields are listed first for that reason.