Skip to content
TypeParser
All tools

JSON to Pydantic

Generate Pydantic models from JSON.

beats jsontopydantic.com edge: Pydantic v2 syntax + nested models
JSON
paste JSON
Guide

About JSON to Pydantic

Build Pydantic v2 <code>BaseModel</code> classes from any JSON sample. Handles nested models (separate classes), Optional fields, List types, and Union types. Output is ready to paste into a Python module — works with FastAPI, Pydantic Settings, and any other Pydantic-based code.

Why generate Pydantic models

Pydantic is the de facto Python validation library — used inside FastAPI, used standalone for config and API schemas, used as the modern dataclass replacement. Hand-typing models from JSON samples is tedious; generating them from a real sample is fast and error-resistant.

What you get

from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field


class Address(BaseModel):
    street: str
    city: str
    state: str
    zip: str


class User(BaseModel):
    id: int
    email: str
    name: str
    tags: Optional[List[str]] = None
    address: Address
    created_at: datetime

Nested objects become separate classes. Optional fields default to None. ISO dates become datetime. Lists of objects become List[Model].

Common workflows

Type an API response. Curl the endpoint, generate the model, paste into your service. Now responses validate at the boundary.

Build a FastAPI request model. Generate from a sample request payload, use as the path operation argument. Free OpenAPI schema generation.

Migrate dataclasses. If your code uses @dataclass, generating Pydantic gives you validation for free with the same syntax.

Compose larger schemas. Generate from each sub-payload separately; combine into a parent model by hand.

What you should still write by hand

  • Field validators — email format, URL regex, custom rules
  • Computed fields@computed_field for derived data
  • Model configmodel_config = ConfigDict(...) for renames, extras
  • Custom serializers@field_serializer for output shaping

The generator gives the structure; you add the constraints.

Frequently asked questions

Pydantic v1 or v2?
v2 by default — it is the modern dominant version. Toggle v1 syntax for older projects.
How are dates handled?
ISO 8601 strings become datetime; date-only strings become date. Pydantic parses these automatically at validation time.
Optional vs default?
Optional means None is a valid value. Default means a value is provided when missing. The generator emits Optional[X] = None for keys that are sometimes missing.
Forward references?
For self-referential models (a tree of comments where each Comment has children: List[Comment]), the generator emits a forward reference and a model_rebuild() call.
Field validators?
Not auto-generated — they require domain knowledge. Add @field_validator by hand for email, URL, range constraints. The generator gives you the type skeleton.
Can I use this with FastAPI?
Yes — Pydantic v2 models double as FastAPI request/response schemas. Generate, paste, use as path-operation type hints.

Related tools

Last updated: 2025-01-15