JSON to Pydantic

Generate Pydantic v2 models from a JSON sample.

Runs in your browser Instant No sign-up
JSON input
Python

Paste, type or drop a file — output appears as you type.

Waiting for input
Processed locally · nothing is uploadedProcessed locally

How it works

Paste a JSON sample — an API response, a webhook payload, a config file — and get Pydantic v2 models:

  1. Every object becomes a class Name(BaseModel), named after its property: customer → Customer, and items → Item for arrays.
  2. Arrays of objects are merged, so a key present in only some items becomes an optional field: gift_wrap: bool | None = None.
  3. Values that are sometimes null become X | None.
  4. Whole numbers become int; if any sample value has a decimal part the field is float.
  5. Empty objects become dict[str, Any], and arrays with no items become list[Any].
  6. Classes are emitted in dependency order, so nested models are defined before the models that use them. Imports (Any, Field, RootModel) are added only when needed.

Example

{
  "orderId": "A-1001",
  "createdAt": "2026-09-27T10:00:00Z",
  "total": 249.9,
  "items": [
    { "sku": "KB-01", "qty": 1, "price": 199.9 },
    { "sku": "MS-02", "qty": 2, "price": 25, "giftWrap": true }
  ],
  "customer": { "id": 7, "email": "linh@example.com", "phone": null }
}

With Root name set to Order:

from pydantic import BaseModel, Field


class Item(BaseModel):
    sku: str
    qty: int
    price: float
    gift_wrap: bool | None = Field(default=None, alias="giftWrap")


class Customer(BaseModel):
    id: int
    email: str
    phone: None


class Order(BaseModel):
    order_id: str = Field(alias="orderId")
    created_at: str = Field(alias="createdAt")
    total: float
    items: list[Item]
    customer: Customer

If the root is an array, you get a RootModel, e.g. Root = RootModel[list[RootItem]].

Options

  • Root name — name of the top-level model.
  • snake_case fields — convert keys to PEP 8 names with aliases. Turn it off to keep the original key names where they are valid Python identifiers.

Common mistakes

  • Generating from a single object. Every field becomes required. Paste an array of several real payloads so keys that are sometimes missing become X | None = None.
  • Whole numbers in the sample, decimals in production. "price": 25 becomes int, and a later 25.5 fails validation because it has a fractional part. Include a decimal value in the sample or change the field to float or Decimal.
  • Keys that collide after conversion. If an object has both userId and user_id, both map to user_id; the second becomes user_id_2, still aliased to its original key.

Validating data

order = Order.model_validate_json(response.text)   # from a JSON string
order = Order.model_validate(payload)              # from a dict
print(order.items[0].gift_wrap)
print(order.model_dump(by_alias=True))             # back to camelCase keys

Invalid data raises pydantic.ValidationError with the path of every failing field.

Refining the output

  • Dates: change created_at: str to datetime and Pydantic will parse ISO 8601 strings for you.
  • Money: use Decimal instead of float for prices.
  • Enums: replace str with Literal["paid", "refunded"] when values are known.
  • To accept both orderId and order_id on input, set model_config = ConfigDict(validate_by_name=True) (Pydantic 2.11+; use populate_by_name=True on older 2.x releases).

Also need types for the frontend? Generate them with JSON to TypeScript or JSON to Zod. For Go services use JSON to Go. Broken sample? Fix it first with JSON Repair.

Frequently asked questions

Which Pydantic and Python versions are supported?

The output targets Pydantic v2 and uses modern type syntax (`list[str]`, `str | None`), so it needs Python 3.10 or newer.

How do camelCase keys work with snake_case fields?

With "snake_case fields" on, `orderId` becomes `order_id` and the original key is kept with `Field(alias="orderId")`. Pydantic validates incoming data by alias, so `Order.model_validate({"orderId": "A-1001", ...})` works. Use `model_dump(by_alias=True)` to serialize back to the original keys.

What happens to keys that aren't valid Python names?

They are converted to a safe identifier and aliased — `first-name` becomes `first_name`, a key like `class` becomes `class_`, and keys starting with a digit get a `field_` prefix. This also applies when snake_case is turned off.

Why is a field typed as `None`?

Every sample value for that field was null, so there is no way to know the real type. Change it to the expected type, for example `str | None = None`.

Is my JSON uploaded?

No. The models are generated entirely in your browser — nothing is uploaded.

Last updated