JSON to Pydantic Model Generator

Generate a starter Pydantic BaseModel with Python type annotations from a JSON sample.

Generate a Pydantic model from JSON

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.

How to use it

  1. Paste a representative JSON object into Input JSON. Real values work better than placeholders, because types are inferred from them.
  2. Click Generate. The Python appears under Generated Python; invalid JSON shows the parser error instead.
  3. Click Copy, paste into your project and refine the types as described below.

How JSON values map to Python types

JSON valueGenerated annotation
stringstr
whole number such as 42int
number with decimals such as 9.5float
true / falsebool
nullOptional[Any] = None
arrayList[...] typed from the first element; an empty array gives List[Optional[Any]]
object under key addressits own class Address(BaseModel)
array of objects under key ordersList[OrdersItem] plus class OrdersItem
key that is not a valid name, e.g. first-name, class, 2fafirst_name, class_, field_2fa with Field(alias=...)

Worked examples

1. A user payload

{"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.

2. The same model after review

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 v1

With 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.

3. A top-level array

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: int

Validate the full list with TypeAdapter(List[RootItem]) in Pydantic v2.

Common problems after generating

Pydantic or another output?

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.

FAQ

Does the output work with Pydantic v2?

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.

Are integer fields typed as int?

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.

Are nested objects turned into classes?

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.

How are keys with dashes or spaces handled?

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.

Why is a field Optional[Any]?

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].

Is my JSON uploaded?

No. The model is generated in your browser with JavaScript, and the sample never leaves the page.