JSON to Python
Paste JSON and get Python dataclasses or Pydantic models with type hints.
Add JSON and click Generate to see the code here.
How JSON to Python works
Generates Python classes with type hints from a JSON sample. Each JSON object becomes a class, and nested objects get their own class named after their key. Classes are listed in the right order, so each one is defined before it is used.
For an array of objects, all items are merged into one class. Keys that only some items have become Optional with a default of None. Change Name or Style after generating and the output updates right away.
Input to JSON to Python
- Paste or type JSON into the input box, or upload a .json or .txt file. Then click Generate.
- Name sets the main class name. Default is RootObject.
- Style picks Dataclass (@dataclass) or Pydantic model (BaseModel).
- Input must be valid JSON. If something is wrong, you see the line and column of the first error. Files up to 20MB, and everything runs in your browser.
Output from JSON to Python
- Python code with the imports it needs, then one class for each object shape.
- Values become str, int, float, bool or List. Null values become Optional, and mixed types become Any.
- Field names are snake_case. With Pydantic, a renamed field keeps the original JSON key with Field(alias=...).
- Copy it, download it as models.py or open it in full screen.