JSON to Python Dataclass
Generate Python dataclasses with type hints from a JSON object
About JSON to Python Dataclass
Generates standard-library dataclasses with type hints and snake_case fields, each with a from_dict classmethod that reads the original JSON keys and builds nested classes. Whole numbers become int, decimals float, nulls and keys missing from some array items Optional, and mixed or empty values use Any; keyword keys gain an underscore, as in class_. Useful for scripts that consume APIs; hints aren't enforced at runtime, and everything runs client-side.
Category: Converters.
Frequently asked questions
Why does each class have a from_dict method?
▾
A dataclass only accepts its field names as arguments, so Root(**data) fails on keys like userName or my-key and leaves nested objects as plain dicts. from_dict maps every JSON key to its snake_case field and builds the nested classes, so call Root.from_dict(json.loads(text)).
Which Python version does the output require?
▾
Python 3.9 or later, because lists and dicts are written as list[str] and dict[str, Any]. The from __future__ import annotations line lets a class refer to classes defined further down the file.
Does the dataclass check the types of incoming data?
▾
No. Type hints aren't enforced at runtime, so a string in an int field passes silently. Run mypy or pyright over your code, or move the classes to a validation library such as pydantic if you need runtime checks.
How do I load a top-level JSON array?
▾
The element class is named Root and RootList is an alias for list[Root], so build the list with [Root.from_dict(item) for item in json.loads(text)]. An array of primitives gets a plain alias instead, such as Root = list[int].