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.

Built and maintained by Udit Agarwal

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

  • Generates Pydantic v2 BaseModel classes from any JSON object or array
  • Infers str, int, float and bool from your sample values
  • Marks nullable and sometimes-missing fields as Optional[...]
  • Promotes nested objects into their own named BaseModel classes
  • Unifies arrays into list[...] types, empty arrays become list[Any]
  • Keeps invalid Python keys via Field(alias=...) so parsing still works
  • Emits clean imports for BaseModel, Field, Optional and Any
  • Copy the result or download it as a .py file

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

  • The output shows an Invalid error with a line and column. — The JSON has a syntax issue such as a trailing comma, single quotes, or an unquoted key. Fix it at the reported line and column, then the models regenerate.
  • A field came out as Optional[Any] instead of a concrete type. — Every sample value for that key was null, so no concrete type could be inferred. Provide a sample with a real value for that field.
  • A field uses Field(alias=...) with a renamed attribute. — That key is not a valid Python identifier or is a reserved word, so it is exposed under a safe attribute name with the original key kept as the alias.
  • A list field came out as list[Any]. — The array in your sample was empty, so no element type could be inferred. Include at least one representative element to get a typed list.

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

  • JSON to TypeScript — Generate TypeScript interfaces from a JSON sample.
  • JSON to Code — Generate Go, Rust, Python, Java, Kotlin, C# and TypeScript types from JSON.
  • JSON Schema Generator & Validator — Generate a JSON Schema from a sample or validate JSON against a schema.
  • JSON Formatter — Beautify, minify and validate JSON with error locations.
  • JSON to Zod — Generate a Zod schema (TypeScript) from a JSON sample.
  • JSON Viewer — Text + collapsible tree viewer with expand/collapse and node paths.

Guides

More JSON tools

  • JSONPath Evaluator — Query JSON with a JSONPath expression and see matched values and paths live.
  • JSON to TOON — Convert JSON to TOON (Token-Oriented Object Notation) and back, with an LLM token savings estimate.
  • jq Playground — Run jq filters against JSON in your browser — map, select, group_by and more, with the real jq engine.
  • TypeScript to Mock JSON — Generate realistic mock JSON from a TypeScript interface or type.
  • JSON Schema Sample Data Generator — Generate realistic sample JSON that conforms to a JSON Schema.
  • JSON to XML Converter — Convert JSON to XML and XML back to JSON, in your browser.
  • NDJSON to JSON — Convert NDJSON / JSON Lines to a JSON array and back, line by line.
  • Sort JSON Keys — Recursively sort JSON object keys alphabetically, with pretty-print and reverse options.
  • CSV to JSON — Convert CSV or TSV to JSON with type inference and nested keys, in your browser.
  • Excel to JSON — Convert an .xlsx or .xls spreadsheet to JSON, with sheet and header options, in your browser.
  • Excel to CSV — Convert an .xlsx or .xls spreadsheet to clean CSV or TSV, in your browser.
  • CSV to SQL — Convert CSV to SQL INSERT and CREATE TABLE statements for MySQL, PostgreSQL, SQLite and SQL Server.

See all JSON tools (29) · All tools