Skip to content
/>codemaxxed.com

JSON to Pydantic

Convert JSON into typed Python models: Pydantic v2 (or v1), dataclasses with from_dict/to_dict, attrs, msgspec Structs or TypedDicts. snake_case fields with aliases, datetime/date/UUID detection, Optional handling, modern or legacy typing syntax, and JSON Lines input.

Runs locally. Nothing is uploaded.

JSON sample

Python

Output appears here as you type.

Ready

How this converter works

Paste one JSON document, an array of records or JSON Lines (one record per line). Every sample is merged into one shape: each distinct nested object becomes its own class, defined before the classes that use it, identical shapes under the same key share a class, and objects keyed by ids, UUIDs or dates become dict[str, Item] instead of a class with a field per id.

A field that is missing from some samples gets a None default; by default a field that is sometimes null does too, or it can stay required-but-nullable, or every field can be optional for PATCH-style payloads. Strings that are always ISO datetimes, dates, times or UUIDs are typed as datetime, date, time and UUID for Pydantic and msgspec, which parse them; EmailStr and HttpUrl are opt-in.

Field names are converted to snake_case, and the original key is kept where it differs: Field(alias=...) plus populate_by_name for Pydantic, msgspec.field(name=...) for msgspec, and the key mapping inside from_dict()/to_dict() for dataclasses and attrs. Keys that are not valid Python — dashes, spaces, leading digits or underscores, keywords, Pydantic's own method names — are always renamed. TypedDicts keep the exact keys and switch to the functional syntax when a key is not an identifier.

Choose the Python version to match your project: 3.8 uses List[str] and Optional[X], 3.9 uses list[str], 3.10+ writes X | None. Models only describe what the sample shows, so review the result: a field present in every example is required even if production sometimes omits it, and a field that was only ever null is typed Any.

Frequently asked

Is this Pydantic v1 or v2?

Both. The default target is Pydantic v2 (BaseModel, ConfigDict, RootModel). The Pydantic v1 target uses Field(..., alias=...), a nested class Config and __root__ models, and makes union members strict so v1's left-to-right coercion does not turn "1" into 1.

How are camelCase keys handled?

With the default snake_case naming, isActive becomes is_active = Field(alias="isActive") and the model gets populate_by_name=True, so it validates the original JSON and can also be built with Python names. Dump with model_dump(by_alias=True) to get camelCase back. Pick "Keep JSON keys" to leave valid names untouched.

Can I generate dataclasses, attrs, msgspec or TypedDict instead?

Yes, use the target switch. Dataclasses and attrs classes come with from_dict() and to_dict() helpers that map renamed keys and build nested classes. msgspec Structs decode with msgspec.json.decode(raw, type=Root). TypedDicts are pure type hints for code that keeps working with plain dicts, using NotRequired for keys that are sometimes absent.

Why is a field typed Any?

Its value was null (or an empty array) in every sample, so there was nothing to infer from. Paste a sample where it has a real value, or edit the annotation by hand.

Can I use this with FastAPI?

Yes. The Pydantic models are ordinary BaseModel classes, so they work as request bodies, response models and nested schemas. Turn on Field examples to have the sample values appear in the OpenAPI docs.

Is my JSON uploaded anywhere?

No. The models are generated in your browser; nothing is sent to a server.