JSON to Pydantic v2 Model

Python / FastAPI

Compile sample JSON payloads into typed Python pydantic.BaseModel schemas with PEP 8 Field(alias=...) mapping and rich scalar validation.

JSON Payload
Valid JSON JSON
models.py
0 models Python 3.10+

About This Tool

FastAPI, Litestar, and modern Python data pipelines rely on Pydantic BaseModel classes to validate incoming JSON payloads. Writing nested models and Field(alias="...") annotations by hand for large camelCase REST responses slows down API integration.

This generator inspects JSON objects or arrays of objects, infers nested child models bottom-up, maps non-Pythonic keys to PEP 8 snake_case attributes, and detects standard library UUID and ISO 8601 datetime strings automatically.

Key Features

  • Pydantic v2 ConfigDict Support: Emits populate_by_name=True in ConfigDict whenever camelCase or hyphenated JSON keys require Field aliases.
  • Smart Format Inference: Recognizes UUIDs, ISO 8601 timestamps, dates, EmailStr, and HttpUrl values and generates exact imports.
  • Multi-Sample Array Merging: Merges fields across JSON arrays and marks keys that appear conditionally or hold null values as optional.

Frequently Asked Questions

What is the difference between Pydantic v2 and v1 output modes?
Pydantic v2 emits model_config = ConfigDict(populate_by_name=True) when field aliases are present. Pydantic v1 emits a nested class Config with allow_population_by_field_name = True.
How does snake_case alias mapping work?
When enabled, camelCase or kebab-case JSON keys like "createdAt" or "user-id" are converted into PEP 8 snake_case attributes (created_at, user_id) with Field(alias="createdAt") so Pydantic validates both the raw JSON key and the Python attribute name.
Which string formats are automatically detected?
When format detection is checked, ISO 8601 timestamps map to datetime.datetime, YYYY-MM-DD dates map to datetime.date, canonical UUID strings map to uuid.UUID, email strings map to EmailStr, and HTTP/HTTPS links map to HttpUrl.
How are arrays of objects with missing keys handled?
If you paste a JSON array of objects, the generator merges all sample objects across the array and marks any key that is missing or null in at least one element as optional (T | None = None).