How to use the JSON to Python Dataclass Converter
- 1. Paste JSONAn API response or config object.
- 2. Pick a styleStandard library @dataclass or a Pydantic BaseModel.
- 3. Copy the codeGenerate, review the types and save as a .py file.
How the classes are built
- Strings →
str, whole numbers →int, decimals →float, true/false →bool. - Nested objects → their own class; arrays →
List[...], with array objects named in the singular. null→Optional[Any].- camelCase and odd keys → snake_case names: fullName → full_name, "pin-code" → pin_code. Python keywords like class get an underscore.
With Pydantic, renamed fields get Field(alias="fullName"), so User.model_validate(data) reads the original JSON keys and validates the types. With dataclasses, a comment shows the original key, because dataclasses do not rename or validate anything themselves.
Worked example
The sample user JSON produces 2 classes, Address and User. User has user_id: int, full_name: str, is_active: bool, rating: float, skills: List[str] and address: Address. Nested classes are written first so the file runs top to bottom without errors.
Which style to pick:
- @dataclass: built into Python, with no installs. Good for internal data. You convert nested dicts yourself.
- Pydantic: validates and converts types when loading JSON, handles nested models automatically, and is used by FastAPI. Install it with
pip install pydantic.
Test the generated code in our Python online compiler; Pydantic is a Pyodide package, so import pydantic may work there too.
Frequently asked questions
Which Python version is needed?
Python 3.7+ for dataclasses. The typing imports (List, Optional) work in all current versions.
Why snake_case?
PEP 8, Python's style guide, uses snake_case for variable and attribute names.
Can it handle a top-level array?
Yes. It merges the objects into one class you can use as List[YourClass].
Is the json to python dataclass converter free?
Yes, it is free. Generating your first classes asks for your name and WhatsApp number once, and after that the json to python dataclass converter and every other Uncodemy tool open without the form.