Skip to content

JSON to Python Dataclass and Pydantic Model Generator

Input JSON
Python Output

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

Frequently Asked Questions

Dataclasses or Pydantic?
Pydantic if the JSON comes from outside your program. `User.model_validate(data)` checks types and builds the nested `Address` objects for you. A dataclass does neither: `User(**data)` fails on the camelCase keys, and even with matching keys `addresses` stays a list of plain dicts. Dataclasses suit data you already trust and want without a dependency.
Is the Pydantic output v1 or v2?
v2. It uses `model_config = ConfigDict(populate_by_name=True)`, which doesn't exist in v1. Without that setting, v2 only accepts the alias (`userId`) on input, so you couldn't build a model in Python with `user_id=42`.
Why is `post_code: Optional[str]` still required?
In Pydantic v2, `Optional[str]` without a default means 'required, but may be None'. That matches the sample: `postCode` was on every address, sometimes null. `is_primary` was missing from one address, so it gets `default=None` and can be left out.
Which Python version do I need?
3.9 or newer, because the output uses built-in generics like `list[Address]` and `dict[str, Any]`. Nullable fields use `Optional[...]` from typing; on 3.10+ you can rewrite them as `str | None` if your style guide prefers it.
Can it generate TypedDict?
No. TypedDict keys have to match the JSON exactly, so camelCase keys would stay camelCase, and you get no validation. If you only need a type for a dict, the dataclass output with the field names changed back is the closest thing here.