JSON to Pydantic Model Generator

Generate Pydantic v2 model classes from a JSON sample instantly in your browser. Your JSON stays on your device.

Your JSON is parsed and converted into Pydantic models entirely in your browser, so it stays on your device and nothing is uploaded to a server.

Need Go, Rust or TypeScript types? Try JSON to Code.

About JSON to Pydantic

This json to pydantic generator turns a sample JSON object or array into ready-to-use Pydantic v2 model classes in seconds. Paste a real API response and it infers each field type, str, int, float and bool, marks nullable fields as Optional, unifies arrays into list[...] types, and promotes every nested object into its own BaseModel referenced by name. Keys that are not valid Python identifiers are kept through Field(alias=...) so the model still parses your original payload. It is built for Python and FastAPI developers who would rather generate pydantic models from json than hand-write them while wiring up a typed client. Everything runs locally in your browser, so the JSON you paste is processed on your device and nothing is uploaded to a server.

Features

How to use the JSON to Pydantic

  1. Paste your JSON object or array into the input box.
  2. Set the Root model name to match your schema, or leave it as Model.
  3. Review the generated Pydantic classes in the output panel.
  4. Copy the result or download it as a .py file.

Example

Input

{ "id": 1, "name": "Ada", "address": { "city": "London" } }

Output

from pydantic import BaseModel


class Address(BaseModel):
    city: str


class Model(BaseModel):
    id: int
    name: str
    address: Address

A nested object becomes its own BaseModel, with the root class last.

Common errors & troubleshooting

Frequently asked questions

Does it generate Pydantic v2 models?
Yes. The output targets Pydantic v2 syntax: each model is a class that subclasses BaseModel, with annotated fields and Field(alias=...) where a JSON key is not a valid Python identifier.
How are nested objects handled?
Each nested object becomes its own BaseModel class, named in PascalCase from the owning key, and the parent references it by type. Classes are emitted in dependency order with the root model last.
How does it map JSON types to Python types?
Strings become str, whole numbers become int, decimals become float, booleans become bool, null produces Optional, arrays become list[...] with a unified element type, and objects become nested BaseModels.
What happens with keys that aren't valid Python names?
A key like first-name or 2fa, or a reserved word like class, is exposed under a safe snake_case attribute and the original key is preserved with Field(alias="...") so the model still parses your payload.
How are arrays and empty arrays converted?
Arrays become list[...] using the unified type of their elements, including a nested BaseModel for arrays of objects. An empty array has no element type to infer, so it becomes list[Any].
Is my JSON sent anywhere when I generate Pydantic models?
No. The JSON to Pydantic conversion runs entirely in your browser, so the data you paste is processed locally and never leaves your device.

Related tools

All ArrayKit tools