JSON to Pydantic v2 model converter
Paste JSON and get Pydantic v2 BaseModel classes: str | None = None for keys that are missing or null, list[...] for arrays, nested models, and Field(alias=...) with populate_by_name for keys that aren't Python names. A standard-library dataclass version and the v1 to v2 differences are included.
Updated
Pydantic v2 models
3 classes, 14 fields
no aliases needed
Pydantic v2 models
from datetime import datetime
from pydantic import BaseModel
class Address(BaseModel):
city: str
pin_code: str
class Order(BaseModel):
order_id: int
total: float
coupon: str | None = None
note: str | None = None
class User(BaseModel):
id: int
first_name: str
is_active: bool
balance: float
created_at: datetime
address: Address
tags: list[str]
orders: list[Order]
# Usage
obj = User.model_validate_json(raw) # from a JSON string
obj = User.model_validate(data) # from a dict
data = obj.model_dump()- In Pydantic v2, Optional[X] or X | None with no default is still a required field that merely accepts None. That is why every optional or nullable field here ends in = None, which makes the key optional too.
- Pydantic v2 comes from `pip install pydantic`. Methods renamed from v1: .dict() is model_dump(), .json() is model_dump_json(), parse_obj() is model_validate(), parse_raw() is model_validate_json(), and class Config became model_config.
How are Pydantic models worked out from JSON?
Each JSON object becomes a class that inherits BaseModel, and each key a typed field: str, int, float, bool, datetime for ISO 8601 date-times, list[T] for arrays and another model for nested objects. For an array of objects, every item is read together, so a key present in only some items, or null in some, becomes optional. A number is int unless any value has a decimal part, and then float.
A key that can be missing or null is written X | None = None. In Pydantic v2 a bare Optional with no default is a required field that accepts None, so the explicit default is what makes the key optional. Keys that aren't valid Python names (camelCase, hyphens, keywords like class) get a snake_case field name and Field(alias="originalKey"), and the model gets populate_by_name so either spelling can be used to build it.
Models are written so each one comes after the models it uses, which Python needs for plain annotations. The dataclass option writes the same structure with the standard library and a from_dict() on each class to build nested objects; it does not check types, so it is a lighter alternative when you already trust the data.
"firstName": "Asha" -> first_name: str = Field(alias="firstName") "coupon": null / key missing -> coupon: str | None = None "tags": ["a", "b"] -> tags: list[str]
What changed between Pydantic v1 and v2 for models?
| Pydantic v1 | Pydantic v2 | Why it matters here |
|---|---|---|
| Optional[str] with no default is optional | Optional[str] with no default is required (accepts None) | Generated fields end in = None |
| class Config: | model_config = ConfigDict(...) | Alias and extra settings use it |
| allow_population_by_field_name | populate_by_name (validate_by_name from 2.11) | Needed when a field has an alias |
| .dict() | .model_dump() | Use by_alias=True to get the JSON keys back |
| .json() | .model_dump_json() | |
| parse_obj(), parse_raw() | model_validate(), model_validate_json() | Usage lines at the end |
| Field(regex=...) | Field(pattern=...) | Not generated, but common when you add rules |
| __root__ models | RootModel | Not needed; a top-level array uses TypeAdapter |
How to turn JSON into Pydantic models
- 1Paste a real responseFor a list, include several items so keys that come and go are seen. A JSON error shows its line and column.
- 2Choose the stylePydantic v2 models for validation, or standard-library dataclasses if you only need typed containers. Set the Python version style and whether unknown keys should be rejected.
- 3Copy the codeSave it as models.py. Install pydantic with pip if you chose the Pydantic output.
- 4Validate your dataCall model_validate_json(raw) for a JSON string, or model_validate(data) for a parsed dict. A mismatch raises a ValidationError that names the field.
Frequently asked questions
Why does Pydantic say my Optional field is required?
What is Field(alias=...) for?
How do I get the original JSON keys back out?
How do I validate a JSON array at the top level?
Should I pick Pydantic or a dataclass?
Why was one of my field names changed?
Can I paste an API response that contains private data?
Sources
- Pydantic docs: Migration guide (v1 to v2 renames, Optional behaviour, populate_by_name / validate_by_name from 2.11) (checked 11 Nov 2026)
- Pydantic docs: Alias (Field(alias=...), validate_by_alias defaults to True, validate_by_name defaults to False) (checked 11 Nov 2026)
- RFC 8259: The JSON Data Interchange Format (checked 11 Nov 2026)
Need a custom dev tool for your team?
We build internal tools, dashboards, APIs, and SaaS platforms. If you can describe it, we can ship it.
Built by CountryDevs · No signup · No ads · We count visits, never what you enter