{ }jsonkit
Format
Validate
Convert
Query
Generate

JSON to Pydantic model generator

Infer Pydantic v2 models with nested classes from sample JSON.
343 B · paste or drop a file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
✓ complete343 B → 549 B0.0 ms

About JSON to Pydantic

Generate Pydantic v2 models from JSON by pasting a sample. The tool writes a class(BaseModel) for the root and a named class for every nested object. Nested classes come first, so the file runs top to bottom without forward-reference errors.

Types come from the values. Whole numbers become int, decimals float, true and false bool, strings str, and null Any. Arrays become list[...], and arrays of objects merge into one list[Model].

Runs in your browser. Nothing is uploaded.

How it works

Nested objects become their own classes

Each nested object becomes a BaseModel class named after its field. Arrays of objects are named from the singular of the field, so posts becomes a Post class used as list[Post]. Python needs a class defined before it is used, so nested classes always appear above the class that uses them.

Optional detection across arrays

With Optional detection on, the generator checks every object in an array. A field missing from at least one element becomes field: X | None = None. The model then accepts records with or without that field.

snake_case fields with aliases

With snake_case fields on, userId becomes user_id and keeps the original key with Field(alias="userId"), so parsing still matches the JSON. Turn it off to keep original key names where they are valid Python identifiers. Keys with spaces, hyphens or leading digits, and Python keywords, are always renamed and aliased.

Minimal imports

The output starts with from pydantic import BaseModel. It adds Field only when an alias is used, and from typing import Any only when a null or empty array needs it. You can paste the result straight into a models.py file.

Frequently asked questions

Is my JSON uploaded anywhere?

No. The conversion runs in your browser and works offline, so it is safe for production payloads.

Which Pydantic version does it target?

Pydantic v2. It uses list[X], X | None and Field(alias=...). The output works with any Pydantic 2.x release and needs Python 3.10 or later for the | union syntax.

Why did my field get a Field(alias=...)?

An alias is added whenever the Python attribute name differs from the JSON key, for example userId turned into user_id, or a key with a space or hyphen. Pydantic then reads the original key while your code uses the clean name.

How are optional fields decided?

With Optional detection on, a field present in some but not all objects of an array is typed X | None = None. A field that is always present stays required. Turn the toggle off to make every field required.

What happens with null values or empty arrays?

A null gives no type to infer, so the field becomes Any, and an empty array becomes list[Any]. Use a sample with real values and filled arrays for more precise types.

Sources & last verified

We checked this tool's behavior and the claims on this page against the specifications below on . The tool runs in your browser, so the output you see is exactly what the engine produced.

Found a wrong result or an outdated claim? Report an error and we will check it again.