All tools

JSON to Pydantic

Converters

Paste a JSON response and get Pydantic v2 models, dataclasses or TypedDicts — camelCase keys mapped to snake_case fields, optional fields worked out from your samples.

  • Converter
  • JSON
  • Runs offline
  • Python
Output
An array of objects is merged
Python
from datetime import datetime
from typing import Any
from pydantic import BaseModel, ConfigDict, Field


class Customer(BaseModel):
    id: int
    email: str


class Item(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    sku: str
    quantity: int
    unit_price: float = Field(alias="unitPrice")
    discount_rate: float | None = Field(default=None, alias="discountRate")


class Order(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    order_id: str = Field(alias="orderId")
    placed_at: datetime = Field(alias="placedAt")
    customer: Customer
    items: list[Item]
    total: float
    notes: Any
    coupons: list[Any]

Python models from a JSON sample

Paste a response from an API and get Python classes that describe it: one model per object, nested ones named after their keys, optional fields where a key is missing from some samples, and int or float worked out from the values. camelCase keys become snake_case fields with an alias, so the model reads the JSON as it is.

A sample shows what was sent, not everything that can be sent. Treat the result as a first draft and check optional fields and unions against the API's documentation.

  • Pydantic v2Validates and converts on load; aliases map JSON keys to Python names.
  • DataclassPlain Python with no validation; you map keys to fields yourself.
  • TypedDictTypes the dict as it is, so keys stay exactly as in the JSON.
  • Optional or NoneX | None = None may be missing; X | None must be present but may be null.

How to use the converter

  1. Paste a JSON sample — or an array of several, to merge them.
  2. Pick the output: Pydantic v2, Dataclass or TypedDict.
  3. Keep Snake Case Fields on for Pydantic to get Python names with aliases; Detect Dates turns ISO strings into datetime.
  4. Read the notes: renamed keys, merged samples and empty arrays are called out.
  5. Copy the code, then load data with Order.model_validate_json(text).

Examples

Model a camelCase API

unitPrice becomes unit_price with an alias, so both names work.

Merge several records

The color only one record has becomes optional.

Handle keywords and odd keys

class, from and 2fa get valid names and keep their JSON keys.

Type a dict without converting it

A TypedDict keeps the keys exactly as they arrive.

Loading data with the model

# Validate a JSON string or an already-parsed dict
order = Order.model_validate_json(response.text)
order = Order.model_validate(response.json())

# Back to JSON with the original camelCase keys
order.model_dump_json(by_alias=True)

Frequently asked questions

Pydantic v1 or v2?

The models use Pydantic v2: ConfigDict, model_validate and X | None types. v1 used an inner class Config and parse_obj; new code should use v2.

Why are my fields renamed?

Python names cannot contain dashes, start with a digit or be keywords such as class. The field gets a valid name, and Field(alias=…) keeps reading the original key. populate_by_name=True lets you use either name.

Why float when most values were integers?

If any sample has a decimal, the field must hold it, so it becomes float. Pydantic still accepts whole numbers for a float field.

Why do dataclasses warn about renamed keys?

Dataclasses have no aliases. A key such as first-name cannot be a field name, so you have to map it when you build the object — or use Pydantic or a TypedDict instead.

Is my JSON sent anywhere?

No. The models are generated in your browser; check the Network tab in your developer tools.

Related tools

Learn more