JSON to Python Class Converter
Paste JSON, get Python classes instantly — dataclass, Pydantic v2, or TypedDict. Correct Optional typing, camelCase aliases, nested classes. 100% in your browser, free.
What is JSON to Python conversion?
JSON to Python conversion turns a JSON sample into ready-to-use Python classes — a standard-library dataclass, a Pydantic v2 model, or a TypedDict — so you never hand-write field definitions for an API response or config file. This Python class generator infers correct types (int, float, str, bool, Optional), turns nested objects into named classes, and adds Pydantic aliases for camelCase keys, all 100% in your browser.
Examples
API response → dataclass
{"id":101,"name":"Ada Lovelace","email":"ada@example.com","active":true,"roles":["admin","user"]} from dataclasses import dataclass
from typing import List
@dataclass
class Root:
id: int
name: str
email: str
active: bool
roles: List[str]
A typical REST payload becomes a ready-to-use @dataclass. Whole numbers infer int, decimals infer float, and arrays become List[str].
Nested object (child class first)
{"repo":"pydantic","owner":{"login":"samuelcolvin","id":100}} from dataclasses import dataclass
@dataclass
class Owner:
login: str
id: int
@dataclass
class Root:
repo: str
owner: Owner
Nested objects become separate, named classes. The child class (Owner) is emitted before the parent so the code runs without from __future__ import annotations.
camelCase → Pydantic v2 alias
{"login":"octocat","publicRepos":15,"createdAt":"2011-01-25"} from pydantic import BaseModel, ConfigDict, Field
class Root(BaseModel):
model_config = ConfigDict(populate_by_name=True)
login: str
public_repos: int = Field(alias="publicRepos")
created_at: str = Field(alias="createdAt")
In Pydantic mode, fields are snake_case with Field(alias=...) mapping back to the original JSON key, and populate_by_name lets you build the model either way — so validation round-trips real API data.
Array of objects → TypedDict (optional key)
{"users":[{"id":1,"nick":"x"},{"id":2}]} from typing import List, Optional, TypedDict
class User(TypedDict):
id: int
nick: Optional[str]
class Root(TypedDict):
users: List[User]
Arrays of objects merge into one element type. A key missing from some items (nick) becomes Optional. TypedDict describes the shape of a plain dict with zero runtime overhead.
How to convert JSON to Python
- 1
Paste your JSON
Drop a JSON object, array, or API response into the input box. Conversion starts instantly.
- 2
Pick dataclass, Pydantic, or TypedDict
Toggle the output style to match your project, and rename the root class from the default Root.
- 3
Copy or download
Grab the generated Python with one click and paste it straight into your code.
Common Use Cases
- Typed API clients
- Turn a sample REST or GraphQL response into Pydantic models or dataclasses for a requests or httpx client without writing field definitions by hand.
- Config and fixture parsing
- Generate models for JSON config files, test fixtures, or webhook payloads you need to load and validate.
- Fast prototyping and type hints
- Paste an unfamiliar payload to instantly see its shape as Python types — a quick way to explore a new API or add TypedDict hints to legacy dict code.
How the conversion works
- Structural inference
- Each object becomes a named class; identical shapes are deduplicated so you get one class, not copies. Arrays of objects are merged key by key, and keys absent from some items become Optional.
- Three idiomatic targets
- dataclass keeps zero dependencies and preserves valid keys for Root(**data); Pydantic v2 adds Field aliases plus populate_by_name for real validation; TypedDict describes a plain dict for static type checkers, using functional syntax when a key is not an identifier.
- Correct, dependency-light typing
- Whole numbers map to int (arbitrary precision, no overflow), decimals and exponents to float, and null-only fields to Optional[Any]. typing imports (Optional, List, Any) are added only when actually used.
- 100% client-side
- Parsing and generation run in your browser with no network calls, so your data stays private.
Tips for clean Python models
- Match the mode to the job
- Use dataclass for internal data holders, Pydantic v2 when you validate or parse untrusted input, and TypedDict when you only need static type hints over existing dicts.
- Paste a representative sample
- Fields are marked Optional only when a sample omits them, and null-only values fall back to Any. A filled-in, complete example yields the most precise types.
- Name your root class
- Set a meaningful root name (User, ApiResponse) instead of the default Root for readable, importable code.
Frequently asked questions
How do I convert JSON to a Python class?
What is the difference between dataclass, Pydantic, and TypedDict output?
How do I generate a Pydantic model from JSON?
How does the dataclass output handle camelCase keys?
How are optional and null fields typed?
What Python type does each JSON value map to?
Does it handle nested objects and arrays of objects?
How are Python keywords and non-identifier keys handled?
How do I use the generated dataclass to parse JSON?
Is my JSON data private and safe?
Is the tool free? Do I need an account?
Related Tools
View all tools →Base64 Decoder & Encoder
Encoding & Formatting
Decode and encode Base64 online for free. Real-time conversion with full UTF-8 and emoji support. 100% private — runs in your browser. No signup needed.
Base64 to Image Converter
Encoding & Formatting
Decode a Base64 string or data URI back into an image in your browser. Preview, read dimensions & MIME, then download as PNG, JPG, GIF, SVG. No upload.
CSV to JSON Converter
Encoding & Formatting
Convert CSV to JSON in your browser. RFC 4180, type inference, header row, big-int safe. 100% private, no upload.
.env to JSON Converter
Encoding & Formatting
Paste a .env file, get JSON instantly. Your database passwords, API keys and tokens never leave your browser — 100% private, no upload, free dotenv parser.
Free HTML Entity Decoder — Unescape HTML
Encoding & Formatting
Decode HTML entities and unescape HTML online — free, no signup, 100% in your browser. Converts named, decimal & hex references back to characters; never uploaded.
Free HTML Entity Encoder — Escape HTML
Encoding & Formatting
Encode HTML entities and escape special characters (< > & " ') online — free, no signup, 100% in your browser. Named, decimal, or hex output; never uploaded.