JSON to Python Dataclass

Paste a JSON sample and get a @dataclass (or Pydantic BaseModel) with type hints for every field, str, int, float, bool, Optional[...] and List[...] where appropriate. Nested objects become their own dataclasses. Output is PEP 8-formatted and ready to paste into a Python file.

How to convert JSON to a dataclass

  1. 1

    Paste the JSON

    A single sample works; multiple samples improve nullability detection.

  2. 2

    Pick the style

    Standard library `@dataclass`, Pydantic `BaseModel`, or `TypedDict` for static type-checking only.

  3. 3

    Pick Python version

    3.9+ for `list[str]` syntax, 3.10+ for `|` union types, 3.8 for `Optional[...]` / `List[...]`.

  4. 4

    Copy the code

    One root class plus nested ones. Paste into a `.py` file and import.

Example output: standard @dataclass

Input:

{ "first_name": "Alice", "age": 30, "email": null, "tags": ["admin"] }

Output (Python 3.10+):

from dataclasses import dataclass, field
from typing import Optional


@dataclass
class User:
    first_name: str
    age: int
    tags: list[str] = field(default_factory=list)
    email: Optional[str] = None

Same input with Pydantic

from typing import Optional
from pydantic import BaseModel


class User(BaseModel):
    first_name: str
    age: int
    email: Optional[str] = None
    tags: list[str] = []

Type mapping

JSON value Python type
string str
integer int
number (decimal) float
boolean bool
null (seen alone) None
null + other type Optional[T]
ISO date string date (Python 3.7+)
ISO datetime datetime
array of one type list[T]
mixed array list[Union[T1, T2]]
object Nested class

Dataclass vs Pydantic vs TypedDict

Flavour When to use
@dataclass Standard-library solution, no runtime validation
Pydantic Runtime validation and coercion (FastAPI, settings)
TypedDict Static type checking only, no instance class
attrs Legacy projects that standardized on attrs

Common mistakes

  • Forgetting default_factory for mutable defaults. A list[str] = [] default is a dataclass trap (shared across instances). Use field(default_factory=list).
  • Optional without default. email: Optional[str] without = None still requires the argument. Match your JSON semantics.
  • Pydantic v1 vs v2. Syntax and behaviours differ (class Config vs model_config, validator vs field_validator). The generator defaults to v2.
  • Union order matters for deserialization. Pydantic tries types in declaration order. Put the most specific first (e.g. int before str) if ambiguity is possible.

Frequently Asked Questions

Dataclass for simple value holders with no validation. Pydantic when you want runtime validation, coercion, or FastAPI integration. TypedDict when you only need static type checking (mypy, pyright) and do not want class instances.

Not for Python 3.10+ where | unions and list[str] syntax are native. Useful for 3.7-3.9 projects to use the newer syntax via PEP 563 string annotations. The generator warns when it’s needed.

Each nested object becomes its own class. The root class references them by name, so you can reuse types. Circular references are detected and flagged.

Yes, if you pick the Pydantic flavour. FastAPI uses Pydantic models as request/response types directly. For internal data shuffling, @dataclass is lighter-weight.

Related Tools

Tool available in other languages