Skip to content
Mediawand

JSON to Python Classes

Give your JSON a proper shape in Python. Paste an example and get typed dataclasses or Pydantic models for every nested object, ready to use with your editor's autocomplete.

How to generate Python classes from JSON

  1. 1Paste a JSON example, ideally with several array items.
  2. 2Choose dataclasses or Pydantic and name the root class.
  3. 3Copy the code or download models.py.

How the types are worked out

Every item of an array is merged into one type, so a key that's missing from some items becomes optional and a value that's sometimes null becomes nullable. Whole numbers and decimals are told apart, nested objects get their own named types (an “items” array gets an “Item” type), and objects with the same shape share one type.

Python specifics

Field names become snake_case and keywords get a trailing underscore (class becomes class_). Pydantic models keep the original key with Field(alias=…); dataclasses note it in a comment. Optional fields default to None and come last, as dataclasses require.

Python version

The hints use list[str] and str | None, which need Python 3.10 or newer. Classes are defined before they're used, so no forward references are needed.

Frequently asked questions

Is my data uploaded?
No. Everything runs in your browser, so the JSON never leaves your device.
Dataclasses or Pydantic?
Dataclasses come with Python and just hold data. Pydantic also validates and converts the data when you load it, and reads JSON directly.
How do I load JSON into a Pydantic model?
With Pydantic 2, use Root.model_validate_json(text), where Root is your root class.

Related tools

Loading tools…