JSON to Python Dataclass and Pydantic Model Generator
Models instead of nested dicts
json.loads gives you dicts, and data["addresses"][0]["postCode"] gets old fast. This
tool writes the classes that give that data attribute access and type hints. Pick dataclasses for the standard
library only, or Pydantic for validation and parsing. Fields are renamed to snake_case for PEP 8, and the JSON
key is kept: as a Field(alias=...) in Pydantic, or as a comment in a dataclass.
Example: a user with addresses (Pydantic)
Input, with the root type name set to User:
{
"userId": 42,
"displayName": "Grace",
"class": "admin",
"lastLogin": "2026-11-09T08:15:00Z",
"addresses": [
{ "city": "Wellington", "postCode": "6011" },
{ "city": "Auckland", "postCode": null, "isPrimary": true }
]
} Output with Style set to Pydantic:
from typing import Optional
from pydantic import BaseModel, ConfigDict, Field
class Address(BaseModel):
model_config = ConfigDict(populate_by_name=True)
city: str
post_code: Optional[str] = Field(alias="postCode")
is_primary: Optional[bool] = Field(default=None, alias="isPrimary")
class User(BaseModel):
model_config = ConfigDict(populate_by_name=True)
user_id: int = Field(alias="userId")
display_name: str = Field(alias="displayName")
class_: str = Field(alias="class")
last_login: str = Field(alias="lastLogin")
addresses: list[Address] class is a Python keyword, so the field is class_ with the alias pointing back at
the real key. Classes are written child-first, so Address exists before User
refers to it and you don't need forward-reference strings.
user = User.model_validate(payload)
user.addresses[1].post_code # None
user.model_dump(by_alias=True) # back to camelCase keys The dataclass version
With Style set to dataclasses, the same input gives @dataclass classes. Fields without a default
are moved above fields with one, because a dataclass raises TypeError otherwise:
@dataclass
class Address:
city: str
post_code: Optional[str] # JSON key: "postCode"
is_primary: Optional[bool] = None # JSON key: "isPrimary"
Dataclasses don't rename keys or build nested objects. You map postCode to
post_code and convert each address yourself, which is why the JSON key is left in a comment.
Worth changing by hand
last_login is typed str because JSON has no date type. Change it to
datetime (from the datetime module) and Pydantic parses the ISO 8601 string for you. A field that
was null in every sample comes out as Any. Values that change type between records become
Union[...]. Pydantic v2 picks the member that fits the value best, so Union[int, str] keeps "7" as a string.
To share the same contract with a non-Python service, use JSON to JSON
Schema. Pydantic can also export one from your edited model with User.model_json_schema().
Same JSON, other languages
Every generator here reads the sample the same way, so a key that is optional or nullable in one language is optional or nullable in the others. Paste the same input into: TypeScript, Zod, Go, Rust, Java, C#, JSON Schema.
Related Tools
JSON to YAML Converter
Convert JSON data to clean, readable YAML format instantly.
JSON to XML Converter
Transform JSON objects and arrays into well-formed XML documents.
JSON to CSV Converter
Flatten JSON arrays into downloadable CSV spreadsheets in one click.
YAML to JSON Converter
Convert YAML configuration files to JSON format instantly.