Plain model
- Input
- {"id":1,"name":"Ana","score":9.5}
- Expected output
- from pydantic import BaseModel class Root(BaseModel): id: int name: str score: float
Basic types as in Python.
json to pydantic model python dataclass
Paste the JSON and pick Python (Pydantic), dataclass or TypedDict. Keys such as firstName get an alias, class becomes class_ and optionals default to None.
Basic types as in Python.
snake_case on the field and an alias with the original key.
note is missing from one document; phone is null in another.
class and from get an underscore, with an alias.
They become datetime and UUID, with the imports.
Required first; the optional gets = None.
A field is optional when it is missing from at least one of the objects observed at that position, whether in different documents or in different items of the same array. If every object in a 50-item array has the key phone, it is required; if one does not, it becomes phone?: string. A null value does not make the field optional: it makes it nullable (string | null). The two concepts are different in APIs and in languages, so the tool keeps them apart. The All fields optional switch marks every field optional, which is useful for PATCH bodies and partial responses.
Pydantic when data comes from outside and needs validation and conversion. Dataclass for simple objects (and immutable ones, with frozen=True) without an extra dependency. TypedDict when you keep working with dicts, for example to annotate the return of json.loads.
In Pydantic, the tool converts to snake_case and records the original alias. In dataclass and TypedDict the keys stay as they are, so Root(**data) and the dict keep working.
With native types turned on, date-time, date and UUID strings become datetime, date and UUID in Pydantic, and Pydantic converts them while validating. In the other variants they stay str.
For the str | None syntax, yes: Python 3.10 or later (list[str] already works since 3.9). On earlier versions, switch to Optional[str] and List[str] from the typing module.
export type Root = RootItem[];
export interface RootItem {
id: number;
number: string;
status: string;
createdAt: string;
total: number;
customer: Customer;
items: Item[];
tags: string[];
shipping: Shipping;
billing?: Shipping;
couponCode?: string;
}
export interface Customer {
id: string;
name: string;
email: string;
phone: string | null;
}
export interface Item {
sku: string;
name: string;
qty: number;
price: number;
}
export interface Shipping {
street: string;
city: string;
zip: string;
}
A string with a few repeated values, in at least 8 samples, becomes an enum.
Date-time, date, UUID, email, URL and IPv4.
Objects with the same fields become one type.
Keys that look like ids or dates become a map, and an empty object becomes a map of unknown values.
A fixed-length array with different types per position, like ["a", 1].
Avoids int when the samples happen to contain only integers.
Rejects trailing commas, comments, single quotes and similar.
Language style: camelCase, snake_case or PascalCase, annotated with the original key. Dataclass and TypedDict always keep the keys.
Go, C#, Java, Swift and Pydantic. In other languages the field stays a string.
readonly in TypeScript, frozen in Python, init in C# classes and let in Swift.
The JSON is processed in your browser. Nothing you paste or open is sent to servers.