JSON to Python Dataclass Generator — Free Online Tool
Convert JSON to Python dataclass definitions with type annotations using AI. Generates @dataclass code with Optional, List, and snake_case field names. Free.
Related Guides
JSON to Python Dataclass Generator — Free Online Tool
About JSON to Python Dataclass Generator — Free Online Tool
This tool converts JSON objects into Python dataclass definitions with accurate type annotations, handling nested objects, arrays, optional fields, and primitive types automatically. Developers use it to eliminate boilerplate when working with APIs, config files, or serialized data — generating production-ready dataclasses in seconds instead of writing them by hand.
JSON to Python Type Mapping
| JSON Type | Example Value | Python Type Annotation | Notes |
|---|---|---|---|
| string | "hello" | str | Always maps to str |
| number (integer) | 42 | int | No decimal point in value |
| number (float) | 3.14 | float | Contains decimal point |
| boolean | true / false | bool | JSON true → Python True |
| null | null | Optional[X] | Wrapped with Optional when field can be null |
| array of strings | ["a","b"] | list[str] | Element type inferred from first item |
| array of objects | [{...}] | list[ClassName] | Nested dataclass generated |
| object | {"key": ...} | ClassName | New @dataclass defined recursively |
| mixed array | [1,"x"] | list[Any] | Falls back to Any when types differ |
| empty array | [] | list[Any] | Cannot infer element type |
Python Dataclass vs Alternatives
| Feature | dataclass | TypedDict | Pydantic BaseModel | attrs |
|---|---|---|---|---|
| Runtime type validation | No | No | Yes | Optional (validators) |
| Requires external library | No | No | Yes (pydantic) | Yes (attrs) |
| JSON serialization built-in | No (use dataclasses.asdict) | No | Yes (.model_dump()) | No |
| Mutable by default | Yes | Yes | Yes | Configurable |
| Supports default_factory | Yes | No | Yes | Yes |
| IDE autocomplete | Yes | Yes | Yes | Yes |
| Python version | 3.7+ | 3.8+ | 3.6+ (v1), 3.8+ (v2) | 2.7+ |
| Inheritance support | Yes | Limited | Yes | Yes |
| Frozen / immutable option | Yes (frozen=True) | No | Yes (frozen=True) | Yes |
Frequently Asked Questions
How do I convert JSON to a Python dataclass with nested objects?
When a JSON field contains a nested object, this tool generates a separate @dataclass for the nested structure and uses it as the type annotation in the parent class. For example, a JSON field "address": {"city": "NYC"} produces an Address dataclass and annotates the parent field as address: Address. You can then import dataclasses and use dataclasses.asdict() to serialize back to a dict.
Does the generated Python dataclass handle Optional fields from JSON null values?
Yes. When a JSON field has a null value, the tool annotates it as Optional[str] (or the appropriate type) and sets its default to None. You will need to import Optional from the typing module in Python 3.9 and earlier; in Python 3.10+ you can use the X | None syntax instead.
How do I serialize a Python dataclass back to JSON?
Use import dataclasses, json followed by json.dumps(dataclasses.asdict(instance)) to convert a dataclass instance to a JSON string. For nested dataclasses, dataclasses.asdict() recursively converts all nested instances to dicts. If you need datetime or custom type serialization, consider using a library like pydantic or dacite instead.
What is the difference between Python dataclass and Pydantic for JSON parsing?
Python's built-in @dataclass decorator provides structure and type hints but does not validate types at runtime — passing a string where an int is annotated will not raise an error. Pydantic's BaseModel validates and coerces types on instantiation, making it better for untrusted JSON input such as API payloads. Dataclasses are preferable when you want zero dependencies and your data is already trustworthy.
How do I load JSON data into a generated Python dataclass?
The simplest approach is to use dacite: from dacite import from_dict; instance = from_dict(data_class=MyClass, data=json.loads(json_string)). Without dacite, you can unpack the dict manually: instance = MyClass(**json.loads(json_string)), but this only works for flat structures. For nested objects you need recursive instantiation or a library like dacite, cattrs, or pydantic.
If jsondecode.com saved you time, share it with your team
Free forever. No ads. No sign-up. Help other developers find it.