Skip to content
Devtoolry

JSON to Python

Generate Python dataclasses or Pydantic models from JSON online, with Optional types, nested classes and aliases for camelCase keys.

Something not working, or missing a feature? Send feedback

About the JSON to Python

The JSON to Python converter turns a JSON sample into Python dataclasses or Pydantic v2 models with type hints. Nested objects become their own classes, written before the classes that use them, and keys that are missing from some items become Optional with a default of None.

Conversion happens in your browser, so your files and records stay private.

How to use the JSON to Python

  1. Paste a JSON sample and set the root class name.
  2. Choose dataclasses or Pydantic models.
  3. Copy the code. With Pydantic, load data with Model.model_validate(data); dataclasses do not convert nested dicts on their own.

Examples

Python dataclasses from a user with projects

Input

{
  "id": 7,
  "fullName": "Grace Hopper",
  "email": null,
  "roles": ["admin"],
  "team": { "name": "Compilers", "size": 4 },
  "projects": [
    { "name": "COBOL", "year": 1959 },
    { "name": "A-0", "year": 1952, "archived": true }
  ]
}

Result

from dataclasses import dataclass
from typing import Any, Optional


@dataclass
class Team:
    name: str
    size: int


@dataclass
class Project:
    name: str
    year: int
    archived: Optional[bool] = None


@dataclass
class User:
    id: int
    fullName: str
    email: Any
    roles: list[str]
    team: Team
    projects: list[Project]

Classes are written before the classes that use them, and fields with a default (archived) come last, as dataclasses require.

Pydantic models with snake_case names

Input

{
  "id": 7,
  "fullName": "Grace Hopper",
  "email": null,
  "roles": ["admin"],
  "team": { "name": "Compilers", "size": 4 },
  "projects": [
    { "name": "COBOL", "year": 1959 },
    { "name": "A-0", "year": 1952, "archived": true }
  ]
}

Result

from typing import Any, Optional
from pydantic import BaseModel, ConfigDict, Field


class Team(BaseModel):
    name: str
    size: int


class Project(BaseModel):
    name: str
    year: int
    archived: Optional[bool] = None


class User(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    id: int
    full_name: str = Field(alias="fullName")
    email: Any
    roles: list[str]
    team: Team
    projects: list[Project]

fullName becomes full_name with an alias, and populate_by_name lets you construct models with either name.

Common mistakes

  • Expecting dataclasses to convert nested data. User(**data) leaves team as a plain dict. Use Pydantic, or build the nested dataclasses yourself, if you need real objects all the way down.
  • Mutable defaults. Never write roles: list[str] = [] in a dataclass. Use field(default_factory=list); in Pydantic a plain default is safe.

Works well with the JSON to Python

Frequently asked questions

Dataclasses or Pydantic?

Pydantic validates and converts nested data (dicts become model instances, strings are checked), which suits API responses. Dataclasses are in the standard library and need no dependency, but do not validate or convert nested values.

Why do dataclass fields keep camelCase names?

So that User(**data) works with the JSON keys as they are. Keys that are not valid Python names (class, first name, 2fa) are renamed, with a comment showing the original key, so map those yourself. Pydantic models use snake_case names with aliases instead, which handles every key automatically.

Which Python version is needed?

Python 3.9 or later, for built-in generic types like list[str]. Pydantic models need Pydantic 2.