How to convert JSON to Python
- Paste a JSON object or array, for example a response from the API you are calling.
- Set the name of the root type and pick a style.
- Copy the generated Python code into your project.
What you get
- Pydantic
BaseModelclasses, or standard librarydataclasses. - snake_case attribute names, with
Field(alias=…)to keep the original JSON keys in Pydantic. - Modern type hints:
list[str]andstr | None(Python 3.10+).
Tips for accurate results
The generator only knows the sample you paste. Include arrays with several items and responses where optional fields are present, so it can tell which keys are always there, which can be null and which numbers have decimals.
Frequently asked questions
Is my JSON sent to a server?
No. The code is generated in your browser, so API responses and other private data never leave your device.
Pydantic or dataclasses?
Pydantic validates and converts data when you create a model, which is ideal for API payloads (it is what FastAPI uses). Dataclasses are part of the standard library and only store data, without validation.
How do I load JSON into the model?
With Pydantic v2 use Root.model_validate_json(text) or Root.model_validate(data). With dataclasses, pass the parsed dict yourself, renaming keys that were changed to snake_case.
Why are the classes in reverse order?
Each class is defined before the classes that reference it, so the file runs top to bottom without forward references.