DevKit
Generator

JSON to Python

Generate Python dataclasses from sample JSON.

Last updated: August 2026

Share:

Was this tool helpful?

Generating dataclasses from JSON

Python can parse JSON into dicts with one call, but a dict gives you no autocomplete, no type checking, and no clear contract. A dataclass with type hints turns a loose payload into a documented structure your editor and type checker understand. This tool reads a sample and emits those classes, nesting a new class for each nested object.

Example

Input:

{ "id": 7, "name": "Ada", "scores": [9.5, 8.0] }

Generated dataclass:

from dataclasses import dataclass

@dataclass
class Root:
    id: int
    name: str
    scores: list[float]

Hints are not enforcement

A frequent misunderstanding: type hints do not validate anything at runtime. If the JSON delivers a string where you annotated int, the dataclass will happily hold it. The hints help your tooling, not your runtime. For actual validation you want a library like Pydantic or a schema check; see the JSON Schema validator.

Optional and null fields

A value that appears as null in the sample should become Optional[...] (or X | None). Generators often infer the non-null type and miss this, so fields that can be absent need a default of None added by hand, or construction will raise when the key is missing.

FAQ

Why list[float] when my sample had an int? If any element is fractional the whole list widens to float. A sample of all whole numbers would instead infer list[int], which may be wrong for real data.

Can I nest classes? Yes. Each nested object becomes its own dataclass, referenced by name from the parent, which keeps the structure readable instead of one giant flat class.

Does field order matter? Dataclass fields with defaults must come after fields without them, so adding Optional defaults can force a reordering.