Output will appear here
Output will appear here
Paste JSON and get Pydantic v2 BaseModel classes, one per object shape. camelCase or dashed keys become snake_case attributes with a Field(alias=...), so Model.model_validate(data) reads the original JSON unchanged. ISO timestamps become datetime, calendar dates become date, and a string that repeats a few values becomes Literal[...]. Missing keys default to None.
By Ankit Aglawe · Updated · Runs entirely in your browser
Each camelCase key becomes a snake_case attribute with an alias, and populate_by_name=True lets you build the model with either spelling. unitPrice is float because 39.9 appears in the list. gift_message is only on one item, so it is Optional[str] with a None default.
{
"orderId": 50213,
"customerName": "Ana Souza",
"shippingAddress": {
"line1": "Rua Augusta 1508",
"city": "São Paulo",
"postalCode": "01304-001",
"country": "BR"
},
"items": [
{ "productId": "SKU-881", "quantity": 2, "unitPrice": 39.9 },
{ "productId": "SKU-112", "quantity": 1, "unitPrice": 120.0, "giftMessage": "Feliz aniversário" }
],
"placedAt": "2026-10-27T18:42:10Z"
}from datetime import datetime
from typing import Optional
from pydantic import BaseModel, ConfigDict, Field
class ShippingAddress(BaseModel):
model_config = ConfigDict(populate_by_name=True)
line1: str
city: str
postal_code: str = Field(..., alias="postalCode")
country: str
class Item(BaseModel):
model_config = ConfigDict(populate_by_name=True)
product_id: str = Field(..., alias="productId")
quantity: int
unit_price: float = Field(..., alias="unitPrice")
gift_message: Optional[str] = Field(None, alias="giftMessage")
class Root(BaseModel):
model_config = ConfigDict(populate_by_name=True)
order_id: int = Field(..., alias="orderId")
customer_name: str = Field(..., alias="customerName")
shipping_address: ShippingAddress = Field(..., alias="shippingAddress")
items: list[Item]
placed_at: datetime = Field(..., alias="placedAt")observed is parsed into a timezone-aware datetime. precip_mm is Optional[float] with no default: the key must be present, but its value may be null. condition repeats two values, so it is Literal["rain", "cloudy"]. The top-level array gives RootList = list[Root], which you can validate with TypeAdapter(RootList).
[
{ "station": "LHR", "observed": "2026-10-28T06:00:00Z", "temp_c": 9.4, "wind_kph": 18, "precip_mm": null, "condition": "rain" },
{ "station": "MAN", "observed": "2026-10-28T07:00:00Z", "temp_c": 9.9, "wind_kph": 21, "precip_mm": 0.6, "condition": "rain" },
{ "station": "EDI", "observed": "2026-10-28T08:00:00Z", "temp_c": 11.2, "wind_kph": 15, "precip_mm": 0.0, "condition": "cloudy" },
{ "station": "BRS", "observed": "2026-10-28T09:00:00Z", "temp_c": 12.5, "wind_kph": 12, "precip_mm": null, "condition": "cloudy" }
]from datetime import datetime
from typing import Literal, Optional
from pydantic import BaseModel
class Root(BaseModel):
station: str
observed: datetime
temp_c: float
wind_kph: int
precip_mm: Optional[float]
condition: Literal["rain", "cloudy"]
RootList = list[Root]Every code generator on this site is run on the same awkward input: a 64-bit id, a float written as 1.0, a null, a date string, a reserved word as a key, an empty array, a nested array and an array of objects whose keys differ. This is the exact output for Pydantic, generated when the page was built.
{
"id": 9007199254740993,
"price": 19.99,
"ratio": 1.0,
"deleted_at": null,
"created_at": "2024-01-15T10:30:00Z",
"class": "premium",
"tags": [],
"matrix": [[1, 2], [3, 4.5]],
"items": [{ "sku": "A1", "qty": 1 }, { "sku": "B2", "qty": 2, "note": "gift" }]
}from datetime import datetime
from typing import Any, Optional
from pydantic import BaseModel, ConfigDict, Field
class Item(BaseModel):
sku: str
qty: int
note: Optional[str] = None
class Root(BaseModel):
model_config = ConfigDict(populate_by_name=True)
id: int
price: float
ratio: int
deleted_at: Optional[Any]
created_at: datetime
class_: str = Field(..., alias="class")
tags: list[Any]
matrix: list[list[float]]
items: list[Item]A key that holds null gets Optional[T] with no default, so Pydantic still raises Field required if the key is absent. A key missing from some objects gets Optional[T] = None. In the edge-case output, Item.note has the default and deleted_at does not.
With no real value to go on, deleted_at becomes Optional[Any]. It accepts anything, so replace Any with the real type (often datetime) once you know it.
Python ints have no size limit, so the 64-bit id validates as 9007199254740993 exactly. ratio: 1.0 is typed int, and Pydantic accepts 1.0 for an int because it has no fractional part; 1.5 would fail. Lists that mix 3 and 4.5 become list[float].
Python will not allow class or from as attribute names, so they become class_ and from_ with Field(..., alias="class"). Keys like 2fa become field_2fa. model_dump(by_alias=True) gives you the original JSON keys back.
2024-01-15T10:30:00Z becomes datetime and 2024-01-15 becomes date, and the imports from datetime are added only when used. Pydantic parses the string into a real object, so created_at.year works right after validation.
An empty array is list[Any] because there is no element to infer from. [[1, 2], [3, 4.5]] is list[list[float]]. A list holding strings, integers and nulls becomes list[Optional[Union[str, int]]].
| JSON value | Python type | Notes |
|---|---|---|
| string | str | Literal[...] for a small repeated set |
| ISO timestamp / date | datetime / date | |
| integer | int | No 2^53 limit |
| float | float | |
| true / false | bool | |
| null | Optional[T] | No default, key still required |
| key missing in some objects | Optional[T] = None | |
| object | class Name(BaseModel) | |
| array | list[T] | list[Any] when empty |
| mixed values | Union[A, B] |
Enter a JSON object in the left editor. Nested objects, arrays, and mixed-type fields are fully supported.
The output pane displays Python Pydantic v2 BaseModel classes with properly typed fields, including List, Optional, and nested model references.
Copy the models to your clipboard or download as a Python file. Drop them into your FastAPI, Django, or Flask project and start validating data immediately.
Paste a sample on the left and copy the classes on the right. Then load data with Root.model_validate(data), or Root.model_validate_json(raw) if you have the JSON string.
v2. It uses model_config = ConfigDict(...) and builtin generics like list[int], which need Python 3.9 or newer. Generated models are validated against their own source JSON with Pydantic v2 in the project test suite.
The attribute is snake_case and the original key is kept as an alias, so validation reads camelCase input. populate_by_name=True is set so you can also pass order_id=... in Python code.
Yes. They are plain BaseModel subclasses, so you can declare one as a request body or response_model. FastAPI serializes responses by alias by default, so clients get the original camelCase keys.
The values repeated across records, which looks like a fixed set such as a status. If new values can appear, change Literal[...] to str, or the model will reject them.
Only pip install pydantic in your own project. The generator itself runs in the browser and your JSON stays on your machine.
Need several formats at once? Convert All shows every output from one paste. Or browse all JSON converters.