Developer

JSON to Pydantic Model

Convert JSON into Python Pydantic v2 model classes.

All tools

JSON

Pydantic v2

from typing import Any, List, Optional
from pydantic import BaseModel


class Profile(BaseModel):
    email: str


class Root(BaseModel):
    id: int
    name: str
    active: bool
    tags: List[str]
    profile: Profile

Frequently asked questions

What changed between Pydantic v1 and v2 models?
v2 renamed methods (dict to model_dump, parse_obj to model_validate), moved config from a Config class to model_config, and replaced @validator with @field_validator. It is also much faster thanks to a Rust core.
How are optional JSON fields typed in a Pydantic model?
Use Optional[T] or T | None with a default of None to indicate the field may be missing or null. Without a default the field is still required even if its type is Optional.
Can Pydantic validate a JSON string directly?
Yes, call Model.model_validate_json(raw_str) to parse and validate in one step instead of json.loads followed by model_validate. It is faster and surfaces JSON errors with the same error model.