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
- Paste a JSON sample and set the root class name.
- Choose dataclasses or Pydantic models.
- Copy the code. With Pydantic, load data with Model.model_validate(data); dataclasses do not convert nested dicts on their own.
Examples
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.
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
- JSON Formatter: tidy and check the JSON before generating types.
- JSON to TypeScript: generate matching TypeScript interfaces for the front end.
- JSON Validator: find syntax errors in the sample first.
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.
Related tools
JSON to CSV
Convert JSON arrays to CSV online with automatic column detection, nested object flattening and custom delimiters. Open the result in Excel or Sheets.
CSV to JSON
Convert CSV to a JSON array online with header detection, type inference and delimiter auto-detection. Turn spreadsheet exports into data for APIs and code.
JSON to XML
Convert JSON to well-formed XML online with a configurable root element and attribute support. Validate the input and download the XML file.
XML to JSON
Convert XML to JSON online, keeping attributes and turning repeated elements into arrays. Malformed XML is reported with its location.
JSON to YAML
Convert JSON to clean, human-readable YAML online. Turn API responses and package files into config for Kubernetes, Docker Compose or CI pipelines.
YAML to JSON
Convert YAML documents to formatted JSON online with syntax validation. Errors are reported by line, so broken indentation is quick to find and fix.