How it works
Paste a JSON sample — an API response, a webhook payload, a config file — and get Pydantic v2 models:
- Every object becomes a
class Name(BaseModel), named after its property:customer→Customer, anditems→Itemfor arrays. - Arrays of objects are merged, so a key present in only some items becomes an optional field:
gift_wrap: bool | None = None. - Values that are sometimes
nullbecomeX | None. - Whole numbers become
int; if any sample value has a decimal part the field isfloat. - Empty objects become
dict[str, Any], and arrays with no items becomelist[Any]. - 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": 25becomesint, and a later25.5fails validation because it has a fractional part. Include a decimal value in the sample or change the field tofloatorDecimal. - Keys that collide after conversion. If an object has both
userIdanduser_id, both map touser_id; the second becomesuser_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: strtodatetimeand Pydantic will parse ISO 8601 strings for you. - Money: use
Decimalinstead offloatfor prices. - Enums: replace
strwithLiteral["paid", "refunded"]when values are known. - To accept both
orderIdandorder_idon input, setmodel_config = ConfigDict(validate_by_name=True)(Pydantic 2.11+; usepopulate_by_name=Trueon 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