JSON to Pydantic model

Convert JSON to Pydantic models: real output, aliases for renamed keys and model_validate_json.

loading JSON to Pydantic model
About this tool

JSON to Pydantic model: from JSON to code

Paste JSON and get Pydantic models: classes that extend BaseModel, one per object, with the type of each field. Unlike a dataclass, a Pydantic model validates the data when it is created, converts compatible values ("42" to 42) and builds the nested models by itself, which is why it is the usual choice for API payloads, FastAPI request bodies and configuration.

This page opens the converter on Pydantic. Paste your JSON, choose the root name and the flavour, and copy the result; everything is generated in your browser and the JSON is never sent anywhere.

A real conversion

The tool opens with this JSON, an order with a customer and two items:

{
  "id": 48213,
  "status": "paid",
  "paid_at": "2026-11-21T14:03:00Z",
  "total": 389.3,
  "coupon": null,
  "customer": { "name": "Ana Souza", "email": "ana@example.com", "vip": true },
  "items": [
    { "sku": "KB-01", "qty": 2, "price": 149.9, "tags": ["usb", "keyboard"] },
    { "sku": "MS-07", "qty": 1, "price": 89.5 }
  ]
}

and this is what it writes for Pydantic:

from __future__ import annotations

from pydantic import BaseModel
from typing import Any


class Customer(BaseModel):
    name: str
    email: str
    vip: bool


class Item(BaseModel):
    sku: str
    qty: int
    price: float
    tags: list[str] | None = None


class Root(BaseModel):
    id: int
    status: str
    paid_at: str
    total: float
    coupon: Any
    customer: Customer
    items: list[Item]

How each JSON value is typed

In the JSONIn Pydantic
stringstr
integerint
decimalfloat
true / falsebool
nullAny
array of itemslist[Item]
field missing in some itemslist[str] or None

The second item of the sample has no "tags" key, and the first has one: that is why the tool treats it as optional.

Awkward keys, null and empty lists

{"user_id": 7, "display name": "Ana", "tags": [], "address": null, "scores": [9.5, 8], "meta": {"2fa": true}, "mixed": [1, "a"]}
from __future__ import annotations

from pydantic import BaseModel, Field
from typing import Any


class Meta(BaseModel):
    f2fa: bool = Field(alias="2fa")


class Account(BaseModel):
    user_id: int
    display_name: str = Field(alias="display name")
    tags: list[Any]
    address: Any
    scores: list[float]
    meta: Meta
    mixed: list[Any]

Names that are not valid Python are rewritten in snake_case ("display name" becomes display_name, "2fa" becomes f2fa) with a comment about the original key. To read the original key add Field(alias="display name") to that field, or an alias generator in the model config.

The other flavour

The same small JSON, {"id": 1, "paid_at": "2026-11-21", "coupon": null}, in the default flavour and in the "typing" one:

from __future__ import annotations

from pydantic import BaseModel
from typing import Any


class Order(BaseModel):
    id: int
    paid_at: str
    coupon: Any
from pydantic import BaseModel
from typing import Optional, Any


class Order(BaseModel):
    id: int
    paid_at: str
    coupon: Any

The typing flavour uses List and Optional for Python before 3.9 and 3.10. Pydantic v2 reads both forms.

Validating the JSON with the model

order = Root.model_validate_json(text)      # Pydantic v2
print(order.customer.name)

# Pydantic v1: Root.parse_raw(text)

When the data does not fit, Pydantic raises a ValidationError that lists every wrong field and where it is, which is exactly what you want to return to the caller of an API.

Common mistakes

  • Forgetting the alias for renamed keys: the model then expects display_name in the JSON and rejects "display name" with a missing-field error.
  • Mixing Pydantic v1 and v2 code: parse_raw and .dict() are v1, model_validate_json and .model_dump() are v2. Check the installed version before copying examples.

How to use the converter

  1. Paste your JSON on the left (or keep the example); a mistake is reported with its line and column.
  2. Set the root name and the flavour; the code updates as you type.
  3. Copy the code or download it as a file.

Frequently asked questions

How do I generate a Pydantic model from JSON?

Paste the JSON and copy the models. Save them in a module and call Root.model_validate_json on the text you receive.

Does it use Pydantic v1 or v2 syntax?

The models only use BaseModel and plain annotations, which work in both. Only the call that loads the data differs, and the examples above show each one.

How do I make a field optional or give it a default?

Write X | None = None for optional, or any default after the type. The tool does this for fields missing in some items, and "All fields optional" does it for every field.

Report a problem