Skip to content
100% local

JSON to Python dataclass

Turn a JSON sample into typed Python classes — dataclass, Pydantic, TypedDict or NamedTuple.

Input
Output

JSON to Python dataclass

Paste a JSON object or array and this tool infers types for every field and generates Python classes with full type annotations. Choose the output style that fits your codebase: @dataclass, Pydantic BaseModel for validation, TypedDict for existing dict-based code, or NamedTuple for immutable records. Nested objects become their own classes in the right order so the file runs as-is; identical objects reuse a single class instead of duplicating.

Options control how conservative or modern the code is. "Optional for null fields" converts null values to Optional types instead of bare None; "Add default values" enables omitting fields when constructing instances with automatic reordering. "Alias invalid field names" sanitizes JSON keys that aren't Python identifiers, preserving the original key through field metadata or Pydantic Field(alias=...) for correct serialization. "list[T] instead of List[T]" switches between modern PEP 585 builtins and older typing.List, while __future__ annotations changes to X | None / X | Y syntax.

Type inference distinguishes strings, integers, floats and booleans, merges mixed array contents into Union types, and gives root arrays a RootItem class with a Root = list[RootItem] alias. Whole numbers without decimals are read as int—there's no way to recover a decimal "36.0" after parsing. Everything runs locally in your browser; your JSON and generated Python never leave your device.

FAQ

Which output styles are supported?
Four: @dataclass (the standard library default), Pydantic BaseModel (for validation and JSON parsing), TypedDict (type hints over a plain dict, no runtime class) and NamedTuple (immutable, tuple-like records).
How are nested objects and arrays of objects handled?
Each distinct object shape becomes its own class, named after the field that contains it and defined before the class that references it. Objects with an identical set of fields and types reuse one class instead of duplicating it — an array of same-shaped records generates a single item class.
What happens to JSON keys that aren't valid Python identifiers?
They're sanitized into a valid attribute name (e.g. "first-name" → first_name). With "Alias invalid field names" on, the original key is preserved through a native alias (field metadata for dataclass, Field(alias=...) for Pydantic) or a trailing comment for TypedDict and NamedTuple, which have no built-in alias mechanism.
Can it tell int and float apart reliably?
It classifies each number by whether it has a fractional part in your sample, but JSON itself doesn't distinguish int from float for whole numbers — a value sent as 36.0 is indistinguishable from 36 once parsed, so it's read as int.
Is my JSON uploaded anywhere?
No. Parsing and code generation run entirely in your browser — the JSON you paste, and the Python it produces, never leave your device.