What is a JSON to Pydantic converter?
Hand-writing models for an API response is slow and error-prone. This tool infers Pydantic models from a JSON sample: nested objects become named types, arrays become typed collections, and null/missing values become optional fields. It runs quicktype's inference engine entirely in your browser, so real API payloads (often containing user data or credentials) never leave your machine.
Other languages from the same JSON
Pydantic mapping notes
- Each nested object becomes its own
BaseModelsubclass, referenced by the parent. - Missing or null fields become
Optional[...] = None; arrays becomeList[...](arrays of objects →List[Model]); ISO 8601 strings becomedatetime. - Keys that aren't valid snake_case Python names (
createdAt,user-id,class) are renamed withField(alias="..."), so the model validates the original JSON and dumps it back withby_alias=True.
Validate JSON with the generated model
raw = '{"id": 42, "name": "workbench", "owner": {"email": "[email protected]", "active": true}}'
root = Root.model_validate_json(raw) # Pydantic v2
root = Root.parse_raw(raw) # Pydantic v1
print(root.owner.email)
data = root.model_dump(by_alias=True) # back to a dict with the original keysFor a JSON array, validate a list in one call with a TypeAdapter (v2):
from pydantic import TypeAdapter repos = TypeAdapter(list[Root]).validate_json(raw_list)
Use it as a FastAPI request or response model
from fastapi import FastAPI
app = FastAPI()
@app.post("/repos")
def create_repo(repo: Root) -> Root:
return repoFastAPI validates the request body against the model, returns a 422 with the exact failing field when it doesn't match, and documents the schema in /docs automatically. Aliased fields are accepted and returned under their original JSON names.
camelCase APIs and aliases
Python style is snake_case, but many APIs send camelCase. The generator keeps your attribute names Pythonic and maps them with aliases:
class Root(BaseModel):
created_at: datetime = Field(alias="createdAt")
user_id: int = Field(alias="user-id")- Validation reads the alias (
createdAt); your code usesroot.created_at. - Dump with
model_dump(by_alias=True)to get the original keys back. - To also construct models by attribute name (
Root(created_at=...)), addmodel_config = ConfigDict(populate_by_name=True)on v2.
Common Pydantic pitfalls
- "Field required" on real data: the API omitted a key your sample always had. Paste several objects so sometimes-missing keys become
Optional. - Extra keys are ignored by default. To reject unexpected fields, set
model_config = ConfigDict(extra="forbid")(v2). - Lax coercion. Pydantic turns
"42"into42for anintfield. UseConfigDict(strict=True)if the types must match exactly.
How to use
- Paste a JSON sample (an API response works well) into the Input pane.
- Generated Pydantic models appear instantly. Nested objects become their own named types.
- Non-standard JSON (single quotes, trailing commas, comments) is auto-repaired first.
- Copy the code into your project.
Examples
JSON → Pydantic models
{
"id": 42,
"name": "workbench",
"owner": { "email": "[email protected]", "active": true }
}from pydantic import BaseModel
class Owner(BaseModel):
email: str
active: bool
class Root(BaseModel):
id: int
name: str
owner: OwnerFAQ
How do I convert JSON to Pydantic types?
Paste any JSON sample. The models are inferred from the values and nesting, entirely in your browser. Nothing is uploaded.
How are nested objects handled?
Each distinct nested object becomes its own named type, referenced from the parent, arrays of objects included.
What about optional or null fields?
Fields that are null or missing in parts of the sample are typed as optional/nullable in the generated code.
Can I paste multiple samples?
Paste an array of objects: the type is inferred from the union of all items, which catches optional fields a single sample would miss.
Does this target Pydantic v1 or v2?
Both. The models are plain BaseModel subclasses using Field(alias=...), which works the same on v1 and v2. On v2 you can optionally switch List[...] to the built-in list[...].
Why does my field have Field(alias=...)?
Its JSON key isn't a valid or idiomatic Python name (camelCase, hyphens, or a keyword like class). The alias maps the original key to a snake_case attribute so validation works without renaming your API.
Dataclasses or Pydantic?
Use Pydantic when data comes from outside (APIs, files, user input): it validates and coerces at runtime. For plain typed containers with no validation, the JSON to Python dataclasses generator is lighter.
Is my JSON uploaded?
No. Generation runs 100% in your browser via quicktype's engine. Nothing is uploaded.