Youtilities

Popular

All tools

JSON to Python Dataclass

Generate Python dataclasses, Pydantic models, or TypedDicts with type hints from any JSON.

Runs in your browser. Nothing is uploaded.

Input

Waiting for JSON

Python
Python types appear here.

What this does

Paste JSON and get Python classes with type hints: @dataclass, Pydantic v2 models, or TypedDict. Nested objects become their own classes, defined before the classes that use them.

Optional and None

A key that is missing from some items, or sometimes null, becomes T | None = None. Dataclass and Pydantic fields with defaults are moved after the required ones, as Python needs. Whole numbers are int, decimals float.

Names

Dataclass and Pydantic fields are snake_case. Pydantic keeps the original key with Field(alias=...); a dataclass has no aliases, so build it from renamed keys yourself. TypedDict keeps the keys exactly and switches to the TypedDict("Name", {...}) form when a key is not a valid identifier.

dataclass, Pydantic or TypedDict?

A dataclass is plain standard library with no validation, and Root(**data) does not convert nested dictionaries into the nested classes; you build those yourself. Pydantic validates and converts the whole tree: Root.model_validate(data) or Root.model_validate_json(text), and model_dump(by_alias=True) writes the original keys back. TypedDict costs nothing at runtime: the data stays a dict, you read it as data["firstName"], and only a type checker uses the hints. Pick Pydantic for data from outside your code, TypedDict for typing existing dicts, dataclass for simple internal records.

Which Python version?

The output uses list[str] and str | None. With the dataclass style the file starts with from __future__ import annotations, so it also runs on older versions. Pydantic evaluates the hints at runtime, so the X | None form needs Python 3.10 or later; on 3.8 or 3.9 use Optional[X] instead. NotRequired, used by the TypedDict style for keys that are sometimes missing, is in typing from Python 3.11; on older versions import it from typing_extensions.

Keywords and odd keys

Python keywords used as keys, such as class or from, get a trailing underscore: class_. Pydantic adds alias="class" so parsing still works. Mixed or empty arrays are list[Any]. Python ints have no size limit, so large ids are safe, but a value that is sometimes 1 and sometimes 1.5 becomes float.

All json tools