UTILS.
100% in-browser
🐍

JSON to Pydantic Model

Turn a JSON sample into Python Pydantic v2 model classes, with toggles for @dataclass or TypedDict output and snake_case field aliasing.

output

About this tool

The JSON to Pydantic Model tool converts a pasted JSON sample into Python model classes. Python and FastAPI developers constantly scaffold Pydantic models from example API responses, and this converter does it instantly in the browser. It parses your JSON, infers Python types recursively, and emits one class per object: strings become str, ISO date-time strings datetime, date-only strings date, integers int, other numbers float, booleans bool, arrays List[T], and nested objects their own model classes named in PascalCase.

All parsing and generation happen locally with JSON.parse and plain JavaScript, so nothing you paste leaves your machine. Child classes are emitted before the classes that reference them (topological order), and the header only imports what is actually used — BaseModel and Field from pydantic, Optional / List / Union / Any from typing, and datetime / date when relevant. from __future__ import annotations is always included so forward references work regardless of order.

Keys that are missing from some sibling objects, or seen as null, become Optional[T] = None. The output-mode toggle switches between Pydantic v2 BaseModel (default), stdlib @dataclass, and typing.TypedDict. Turn on snake_case fields to rename camelCase or invalid keys and auto-add Field(alias="originalKey") so the model still parses the original JSON. Copy the code or download it as a .py file.

Frequently asked questions

Which Python types are inferred?
string to str, ISO date-time to datetime, date-only to date, integer to int, non-integer number to float, boolean to bool, array to List[T] with T merged from the elements, and object to a nested BaseModel class. Null or missing values become Optional[T] = None.
Can I get a dataclass or TypedDict instead?
Yes. The output-mode toggle emits Pydantic v2 BaseModel (default), a stdlib @dataclass with the dataclasses import, or a typing.TypedDict class. The type inference is the same; only the class shell and imports change.
How are non-identifier or camelCase keys handled?
With snake_case fields on, a key like userName becomes user_name plus Field(alias="userName") so it still round-trips. Keys that are Python keywords or invalid identifiers are always aliased so the generated code is valid.
Are the classes ordered correctly?
Yes. Nested child classes are declared before the parent classes that use them, and from __future__ import annotations is included, so the models compile without forward-reference errors.

More tools