/tools/json-to-python
JSON to Python Converter
Generate Python 3.10+ Pydantic v2 models, keyword-only dataclasses or precise TypedDict definitions.
JSON sample
Python output
Validate a JSON string with a generated Pydantic model
Choose Pydantic v2 output, name the root Account and generate from:
{"name":"Ada","age":37}
The model has name: str and age: int. After copying the generated model into a project with Pydantic v2, use Account.model_validate_json(payload) for a JSON string or Account.model_validate(data) for an already parsed dictionary.
Review required fields and aliases against the API contract. Type inference observes the supplied examples, so add representative responses before using the generated model as a validation boundary.
/usage
Generate Python models from real JSON
- Paste a representative API response or choose a JSON file.
- Select Pydantic v2, dataclasses or TypedDict output and name the root model.
- Generate, review uncertain unions or empty arrays, then copy or download the Python file.
Generate Pydantic v2 models with field aliases
Paste this synthetic JSON, keep the root model Root and choose Pydantic v2. Select Generate Python to infer models from all three users.
{
"users": [
{
"user-id": 42,
"label": null
},
{
"user-id": 43,
"label": "Ada"
},
{
"user-id": 44
}
]
}
The complete generated model definitions are:
from __future__ import annotations
from pydantic import BaseModel, ConfigDict, Field
class User(BaseModel):
model_config = ConfigDict(populate_by_name=True)
user_id: int = Field(alias="user-id")
label: str | None = None
class Root(BaseModel):
model_config = ConfigDict(populate_by_name=True)
users: list[User]
user_id is a valid Python identifier with an alias for the original JSON key user-id. The missing label becomes a field with default None, while the observed null is included in str | None. In this generated model, a missing label and an explicit null both read as None; use Pydantic's model_fields_set if you need to distinguish whether the key was supplied.
With Pydantic v2 installed, place this code after the generated definitions and pass the original JSON as json_text:
value = Root.model_validate_json(json_text)
print(value.users[0].user_id) # 42
print(value.model_dump(by_alias=True)["users"][0]["user-id"]) # 42
Sample-based inference cannot establish unseen types, business constraints or custom validators. Review the output before integration. Keep the existing dataclass and TypedDict options when those shapes suit your project; they do not provide Pydantic's runtime validation. For static JavaScript types, generate TypeScript interfaces.
/inference
Python output that preserves the evidence
Pydantic v2 by default
Renamed fields use aliases and ConfigDict(populate_by_name=True), so awkward JSON keys remain serialisation-safe.
Missing differs from null
Fields missing from some array objects become optional. Explicit JSON nulls remain visible in the generated type.
Alternative standard-library shapes
Keyword-only dataclasses and TypedDict output support projects that do not use Pydantic.
Bounded local work
The shared inference worker records shape evidence rather than duplicate values, with depth, traversal, schema and output limits.
/choose output
Pydantic, dataclass or TypedDict?
| Python output | Best fit | Generated behaviour | Representative case |
|---|---|---|---|
| Pydantic v2 | Runtime validation and API boundaries | BaseModel, field aliases and populate_by_name | |
| Dataclasses | Standard-library data containers | Keyword-only classes with safe field ordering | |
| TypedDict | Static typing for dictionary-shaped data | Required and NotRequired keys without runtime conversion |
Use more than one representative object when fields vary. Empty or mixed arrays correctly produce uncertainty that should be reviewed rather than guessed. The public inference fixtures and reproduction notes cover nested, optional, aliased and uncertain shapes.
/faq
JSON to Python questions
Does it support nested arrays and objects?
Yes. Every sampled array item contributes evidence to deterministic nested models.
Why does an empty array use Any?
An empty array contains no element evidence. Add a representative item or replace Any with the domain type after generation.
Is generated code a finished domain model?
It is a strong starting contract from observed data. Review semantic constraints, validators and domain naming before production use.