TXT · Text & Data tools

JSON to Python Dataclass Converter

Top-level type
Python
JSON input
Python output
Inference
PythonlanguageJSON sampleinput0types0lines out0chars out
Go to the full JSON to Types generator with all ten languages

Shareable link

Settings are written to the URL as you change them. Nothing differs from the defaults yet.

Python classes from a JSON sample

Paste a JSON document and get typed classes for it. The default is a @dataclass per object shape with converter helpers; one checkbox switches to pydantic models, which validate on construction and need no helpers.

Example

With pydantic BaseModel and Classes only checked, targeting Python 3.10:

from pydantic import BaseModel


class Order(BaseModel):
    sku: str
    qty: int
    gift: bool | None = None


class Customer(BaseModel):
    id: int
    name: str
    tags: list[str]
    orders: list[Order]

gift is bool | None = None because only some orders in the sample carry it. Nested classes are declared first so forward references are not needed.

Dataclasses

Without pydantic, each class is a @dataclass and the file ends in from_dict/to_dict static methods plus small type-checking functions. The dataclass path has no dependency outside the standard library, which is its reason to exist; pydantic is the choice when the data comes from users or the network and should be validated rather than asserted.

Python version

The Python select changes only the hint syntax. 3.10 is the shortest; 3.9 keeps Optional[] but uses builtin generics; 3.7 is the oldest with dataclasses. Pick the lowest version your runtime supports.

Limits

Inference reads string content. A postal code such as "75001" becomes int unless Integers from numeric strings is turned off under Inference, and pydantic will then reject the real string value. Dates become datetime when date inference is on; mixed-type fields become Union[...] or X | Y.

Frequently Asked Questions

Dataclasses by default, decorated with @dataclass and followed by from_dict and to_dict helpers. Check pydantic BaseModel to emit classes that inherit from BaseModel instead, with no helpers because pydantic parses on its own.

It decides the syntax of the hints. 3.10 writes bool | None and list[str]; 3.9 writes Optional[bool] with list[str]; 3.7 and 3.6 fall back to typing.List and typing.Optional; 3.5 emits no hints at all.

A key missing from some objects becomes X | None = None (or Optional[X] = None), placed after the required fields so the dataclass field order is valid.

Yes, by default snake_case properties rewrites camelCase keys to snake_case. The from_dict helper reads the original key; a pydantic model gets a Field alias when the names differ.

They convert a parsed dict into the dataclass tree and back, checking each value's type along the way with assert statements. Check Classes only to leave them out.

Generate types for one language

Full JSON to Types Generator tool

Explore Our Tools

Browse all tools