01What is JSON to Pydantic?
Writing Pydantic models by hand for a big API payload is slow, and it's easy to miss a nested field. Paste a JSON sample here and you get BaseModel classes back, in either v1 or v2 syntax. The generator walks the parsed JSON once. Strings become str, whole numbers int, other numbers float, and booleans bool. A null can't tell us much, so it becomes str | None in v2 or Optional[str] in v1. Arrays turn into list[...] in v2 and List[...] in v1. When an array mixes whole and decimal numbers, the type widens to float. Truly mixed arrays become a union like list[int | str], and empty arrays fall back to list[Any]. Each nested object gets its own class, named in PascalCase from its key. Child classes are printed before the parent, so the file imports cleanly. If two objects have exactly the same fields, they share one model. That's why a list of orders gives you a single OrdersItem. Field names follow Python's identifier rules. camelCase keys become snake_case, Python keywords get a trailing underscore, and keys starting with a digit get a field_ prefix. Leading underscores are dropped too, because Pydantic treats underscored attributes as private and skips them. Whenever the Python name differs from the JSON key, the field gets Field(alias="key"). The model also enables population by field name, through ConfigDict in v2 or an inner Config class in v1, as described in the Pydantic docs on aliases. Here's a quick example. The input {"_id":"x","isActive":true,"2fa":false} in v2 mode produces three aliased fields: id: str, is_active: bool and field_2fa: bool. The class also gets model_config = ConfigDict(populate_by_name=True). A top-level array becomes a type alias, such as Root = list[RootItem]. An empty object gives a class with pass. You can rename the root class with the Model name box. There are limits. The tool only sees one sample, so a field that is sometimes missing still looks required. Nullable fields get no default either. When array items have different keys, you get a union of near-identical models rather than one model with optional fields. The import lines are fixed, so unused names like Union may appear and need a quick cleanup. Parsing and generation happen in your browser tab, and your JSON is not uploaded.
Python backend developers who build FastAPI or Django services, plus AI engineers who validate model output, use it to get typed Pydantic models from a JSON sample in seconds. The tool runs entirely in your browser, so your data never leaves your device.
02How to use JSON to Pydantic
- Paste a representative JSON object or array into JSON Input, or pick one of the built-in examples
- Type the class name you want in Model name (it defaults to Root)
- Click v1 or v2 to match the Pydantic version installed in your project
- Read the generated classes in Pydantic Output and check the types the tool guessed
- Click Copy, paste the code into your module and delete any imports you don't use
03Worked examples
- Input:
{"user_id":7,"full-name":"Ann","tags":["a","b"],"meta":null} (v2) - Result:
from pydantic import BaseModel, Field, ConfigDict from typing import Any, List, Optional, Union class Root(BaseModel): model_config = ConfigDict(populate_by_name=True) user_id: int full_name: str = Field(alias="full-name") tags: list[str] meta: str | None - full-name isn't a valid Python name, so it gets an alias. The null field is typed str | None until you know better.
- Input:
{"user_id":7,"full-name":"Ann","tags":["a","b"],"meta":null} (v1) - Result:
class Root(BaseModel): class Config: allow_population_by_field_name = True user_id: int full_name: str = Field(alias="full-name") tags: List[str] meta: Optional[str] - Same input in v1 mode. You get an inner Config class and typing.List and Optional. The import lines are trimmed here.
- Input:
{"_id":"x","isActive":true,"2fa":false} (v2) - Result:
class Root(BaseModel): model_config = ConfigDict(populate_by_name=True) id: str = Field(alias="_id") is_active: bool = Field(alias="isActive") field_2fa: bool = Field(alias="2fa") - The leading underscore is dropped so Pydantic doesn't treat _id as private. camelCase and digit-first keys are fixed the same way. Imports trimmed.
- Input:
{"class":1,"items":[{"id":1},{"id":2,"qty":3}]} (v2) - Result:
class ItemsItem(BaseModel): id: int class ItemsItem1(BaseModel): id: int qty: int class Root(BaseModel): model_config = ConfigDict(populate_by_name=True) class_: int = Field(alias="class") items: list[ItemsItem | ItemsItem1] - class is a keyword, so the field becomes class_. Items with different keys give two models in a union. Imports trimmed.
- Input:
{"orders":[{"id":1},{"id":2}],"nums":[1,2.5],"mix":[1,"a"],"e":[],"o":{}} (v2) - Result:
class OrdersItem(BaseModel): id: int class O(BaseModel): pass class Root(BaseModel): orders: list[OrdersItem] nums: list[float] mix: list[int | str] e: list[Any] o: O - Same-shaped orders share one model. 1 and 2.5 widen to float, an empty array is list[Any] and an empty object becomes a pass class. Imports trimmed.
- Input:
[{"id":1,"n":"x"},{"id":2,"n":"y"}] (v2) - Result:
class RootItem(BaseModel): id: int n: str Root = list[RootItem] - A top-level array becomes a type alias. Use it with TypeAdapter(Root) in v2 to validate the whole list. Imports trimmed.
04Common use cases
- Writing request and response models for a FastAPI route from a real payload you captured in your browser's network tab
- Defining a schema for structured LLM output, then validating each model reply with model_validate_json
- Typing a Stripe or GitHub webhook body in a Django or Flask view instead of poking at nested dicts
- Moving an old v1 codebase to v2 by generating both versions from the same sample and comparing them
- Turning a JSON config file into a typed settings model that fails fast on a bad value
- Handling MongoDB documents whose _id key would otherwise be ignored by Pydantic as a private attribute
05Tips
- In v2, a nullable field like meta: str | None has no default, so the key is still required. Add = None if the API sometimes leaves it out.
- Feed the tool an item that has every optional key filled in. Otherwise you get ItemsItem and ItemsItem1 in a union, which you'll want to merge by hand.
- The v2 output uses X | None and list[...] syntax, which needs Python 3.10 or newer at runtime.
- Pydantic 2.11 added validate_by_name and validate_by_alias in ConfigDict. They do the same job as populate_by_name, so switch if your team prefers the newer names.
- Dump with model_dump(by_alias=True) when you send data back to the API, or you'll get the snake_case names instead of the original keys.
06Related concepts
Pydantic BaseModel, FastAPI, type hints, field alias, data validation.
Frequently Asked Questions
What is JSON to Pydantic?
JSON to Pydantic is a free, browser-based developer tool on AIDToolStack. Convert JSON to Pydantic models It requires no installation or sign-up and works entirely in your browser.
Is JSON to Pydantic free to use?
Yes. No sign-up, no fees and no usage cap.
Does JSON to Pydantic store my data?
No. It runs in your browser; nothing is uploaded.