JSON to Python dataclass
Paste a JSON sample and get Python dataclasses, instantly and entirely in your browser. Optional fields, nullability and nested types are inferred from the sample — and anything the sample cannot settle is reported rather than guessed.
Type definitions will appear here
Python dataclasses from a sample
Generates @dataclass declarations with typing hints. Keys that are not valid Python identifiers are renamed and noted.
For runtime validation rather than plain type hints, use the Pydantic version.
What a sample cannot tell you
Types are inferred from one sample. A field absent from your sample will be absent from the output, and a field that happens to hold only whole numbers will be typed as an integer even if it can hold decimals. Paste the widest sample you have. For a guarantee rather than an inference, start from a JSON Schema.
What Python output looks like
Generated from two sample records, one of which has an extra field and a null. This is the actual output, not an illustration:
from dataclasses import dataclass
from typing import Any, List, Optional
@dataclass
class Address:
city: str
postcode: Optional[Any] = None
@dataclass
class Root:
id: int
user_name: str
# JSON key: "userName"
is_active: bool
# JSON key: "isActive"
score: float
tags: List[str]
address: Address
last_seen: str
# JSON key: "lastSeen"
nickname: Optional[str] = None
The three things that differ by language
Large integers
Python integers are arbitrary precision, so 9223372036854775807 round-trips exactly through json.loads. This is one of the few runtimes where a 64-bit identifier is safe by default.
Optional against nullable
Absent fields become Optional[T] with a None default. Dataclass ordering puts fields with defaults last, as the language requires.
Names that are not identifiers
Keys that are not valid identifiers are renamed to snake_case and the change is reported. For a model that maps back to the original key, use the Pydantic version.