JSON to Pydantic Model

Generate Pydantic BaseModel classes from a JSON sample.

Open tool

Overview

Paste a JSON sample and the generator emits Pydantic BaseModel classes with typed fields and automatic alias mapping for snake_case vs camelCase. Nested objects become their own models; arrays of objects use list[Model] annotations.

It's for Python developers building FastAPI services, validating webhook payloads, or modelling responses from third-party APIs. Reach for it when scaffolding a typed model from a real-world response, replacing Dict[str, Any] placeholders in legacy code, or producing models compatible with FastAPI's OpenAPI generation.

How it works

The generator walks the JSON tree and produces a BaseModel subclass per object. Field types are inferred: integers map to int, floats to float, booleans to bool, strings to str, lists to list[T], and nulls widen to Optional[T]. The output targets Pydantic v2 syntax by default - Field(..., alias="...") for snake_case keys, model_config = ConfigDict(populate_by_name=True) to accept either form.

Type narrowing happens per the most-restrictive observation: if every value in a list is an integer, the field is list[int]; if mixed, list[Any].

Examples

  • Input:
    {"id": 1, "name": "Alex", "is_active": true}
    
    Output:
    from pydantic import BaseModel, ConfigDict, Field
    
    class User(BaseModel):
        model_config = ConfigDict(populate_by_name=True)
        id: int
        name: str
        is_active: bool = Field(alias="is_active")
    
  • Nested model:
    class Address(BaseModel):
        city: str
        zip: str
    
    class User(BaseModel):
        id: int
        address: Address
    
  • Optional field:
    note: str | None = None
    
  • List of objects -> items: list[Item].

FAQ

Pydantic v1 or v2?

Output targets v2 (model_config, field_validator, modern union syntax). For v1, manually rewrite ConfigDict to a nested Config class and use Optional[T] instead of T | None.

How are camelCase keys handled?

Field names use Python snake_case with alias="camelCase" annotations. With populate_by_name=True, models accept either form.

Does it generate validators?

No - the model only has type annotations. Add @field_validator or @model_validator decorators manually for custom logic.

How does it handle dates?

ISO 8601 strings are detected and typed as datetime.datetime. Plain dates become datetime.date. Pydantic v2 parses both formats automatically.

Try JSON to Pydantic Model

An unhandled error has occurred. Reload ×