JSON to Pydantic Model Generator
Generate a starter Pydantic BaseModel with Python type annotations from a JSON sample.
Generate a starter Pydantic BaseModel with Python type annotations from a JSON sample.
JSON to Pydantic turns a JSON example into Python source for a BaseModel class. The output starts with the imports it needs, then declares one class per JSON object, nested objects included, ordered so every class is defined before it is used. You can paste it into a module and begin validating API payloads, webhook bodies or config files with Pydantic.
A generated model is a starting point rather than a finished contract. One sample cannot show which fields are optional, which numbers can have decimals or which strings are really enums, so review the result against several real responses. Everything happens in the browser and your JSON stays local.
| JSON value | Generated annotation |
|---|---|
| string | str |
whole number such as 42 | int |
number with decimals such as 9.5 | float |
true / false | bool |
null | Optional[Any] = None |
| array | List[...] typed from the first element; an empty array gives List[Optional[Any]] |
object under key address | its own class Address(BaseModel) |
array of objects under key orders | List[OrdersItem] plus class OrdersItem |
key that is not a valid name, e.g. first-name, class, 2fa | first_name, class_, field_2fa with Field(alias=...) |
{"id":42,"name":"Ada","first-name":"Ada","manager":null,"address":{"city":"London","zip":"N1"},"tags":["admin"],"orders":[{"sku":"A1","qty":2}]}Generated Python:
from typing import Any, List, Optional
from pydantic import BaseModel, Field
class Address(BaseModel):
city: str
zip: str
class OrdersItem(BaseModel):
sku: str
qty: int
class Root(BaseModel):
id: int
name: str
first_name: str = Field(alias="first-name")
manager: Optional[Any] = None
address: Address
tags: List[str]
orders: List[OrdersItem]Nested classes come first so the file runs as-is, id is an int because the sample was a whole number, first-name keeps working through an alias, and manager defaults to None because the sample was null.
The generated file validates the sample already. Tighten what one sample cannot know, such as the real type of manager:
from typing import List, Optional
from pydantic import BaseModel, Field
class Address(BaseModel):
city: str
zip: str
class OrdersItem(BaseModel):
sku: str
qty: int
class Root(BaseModel):
id: int
name: str
first_name: str = Field(alias="first-name")
manager: Optional[str] = None # was Optional[Any]
address: Address
tags: List[str]
orders: List[OrdersItem]
user = Root.model_validate(payload) # Pydantic v2; use Root.parse_obj(payload) on v1With id: int, Pydantic v2 rejects 42.5. If a number field can carry decimals in other responses even though your sample was whole, change int to float.
If the sample is an array, the model describes its elements and is named RootItem:
[{"event":"click","x":10.5,"y":3}]becomes
from typing import Any, List, Optional
from pydantic import BaseModel, Field
class RootItem(BaseModel):
event: str
x: float
y: intValidate the full list with TypeAdapter(List[RootItem]) in Pydantic v2.
field_ prefix, and Python keywords such as class get a trailing underscore. Each renamed field gets Field(alias=...), so the original JSON key still validates. Pass by_alias=True to model_dump() to write the original names back out.null get = None. Add = None or a default to any other field that some responses leave out.List[Optional[Any]]. If later items have extra keys or different types, widen the annotation by hand.Choose Pydantic when you want runtime validation with clear error messages, for example in FastAPI request bodies. If you need plain standard-library classes instead, the JSON to Python dataclass generator does that. For a language-neutral contract, generate a schema with the JSON Schema generator and test payloads against it in the JSON Schema validator. Frontend teams can produce matching interfaces with JSON to TypeScript. If the sample will not parse, fix it first in the JSON validator.
Yes. It uses BaseModel, Field and the typing names Any, List and Optional, which work in Pydantic v1 and v2. Validate with model_validate() on v2 or parse_obj() on v1.
It is not, unless the sample had decimals. Whole numbers such as 42 become int and numbers such as 9.5 become float. If a field is whole in your sample but can carry decimals elsewhere, change it to float.
You should not: every nested object gets its own class, named in PascalCase after its key (Address, OrdersItem), and classes are written before the model that uses them. If two keys would produce the same class name, the second gets a number, such as Item2.
Characters that are not letters, digits or underscores become underscores, so first-name becomes first_name, and the generator adds Field(alias="first-name") so Pydantic still reads the original key.
The sample value was null, so the real type is unknown. The field defaults to None; replace Any with the expected type, for example Optional[str].
No. The model is generated in your browser with JavaScript, and the sample never leaves the page.