#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
⚡ FREE · DATACLASS OR PYDANTIC

JSON to Python Dataclass: Typed Python Classes from JSON

Paste JSON and get @dataclass or Pydantic classes with proper type hints.

  • 🐍 Type hints
  • 🪆 Nested classes
  • 🐍 snake_case names

How to use the JSON to Python Dataclass Converter

  1. 1. Paste JSONAn API response or config object.
  2. 2. Pick a styleStandard library @dataclass or a Pydantic BaseModel.
  3. 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.

More free tools

See all free tools