Code generator

JSON to Python Classes

Turn a JSON example into typed Python: dataclasses, Pydantic models or TypedDicts, with nested classes and modern type hints.

How to use it

  1. Paste a JSON example, or press Sample.
  2. Choose the output style: dataclass, Pydantic or TypedDict.
  3. Copy the code or download models.py.

Which style should I pick?

  • dataclass — standard library, no dependencies. Good for plain data containers. It does not validate types at runtime.
  • Pydantic — validates and converts incoming data, and is used by FastAPI. Load JSON with Root.model_validate_json(text). Keys that aren't valid Python names keep their original spelling through Field(alias=...).
  • TypedDict — describes the dictionaries that json.loads already returns, so it adds type checking without changing your data. Keys that aren't valid identifiers use the functional TypedDict(...) form.

How types are chosen

Strings become str, whole numbers int, decimals float, booleans bool, arrays list[...] and objects their own class. Values that can be null get | None, and keys missing from some array items become optional with a default of None (or NotRequired in a TypedDict). The generated code uses the modern X | Y syntax, so it needs Python 3.10 or later (3.11+ for NotRequired).

Field names are converted to snake_case. Python keywords such as class get a trailing underscore (class_).

Frequently asked questions

Does the dataclass parse JSON for me?

No. Dataclasses don’t convert dictionaries automatically. Use Root(**data) for flat data, or pick the Pydantic style, which parses nested JSON and validates types.

Which Pydantic version is this for?

Pydantic v2. Load JSON with Root.model_validate_json(text) or a dict with Root.model_validate(data).

Why does a field have a comment with the JSON key?

The key isn’t a valid Python name, so it was renamed (for example created-at to created_at). The comment records the original key so you can map it.

What Python version do I need?

Python 3.10 or later, because the code uses the X | Y type syntax. TypedDict output with optional keys needs 3.11+ for NotRequired.

Is my data uploaded?

No. Everything runs with JavaScript inside your browser tab. Nothing you paste is sent to a server or stored.