JSON to Python Converter
Generate Python classes from JSON data. Supports dataclasses, Pydantic, and TypedDict:
Generate Python Classes from JSON
This tool converts JSON data into type-annotated Python classes. Choose from dataclasses, Pydantic models, or TypedDict depending on your use case.
Output Formats
dataclass (Python 3.7+)
Standard library solution for data classes. Simple, no dependencies:
from dataclasses import dataclass
from typing import List, Optional
@dataclass
class User:
id: int
user_name: str
email: str
tags: List[str]
profile: Optional[Profile] = NonePydantic
Powerful data validation library, popular with FastAPI:
from pydantic import BaseModel, Field
from typing import List, Optional
class User(BaseModel):
id: int
user_name: str = Field(..., alias="userName")
email: str
tags: List[str]
class Config:
populate_by_name = TrueTypedDict
Type hints for dictionaries, useful for type checking without runtime overhead:
from typing import TypedDict, List
class User(TypedDict):
id: int
user_name: str
email: str
tags: List[str]When to Use Each Format
| Format | Best For | Features |
|---|---|---|
| dataclass | General purpose, no deps | Auto __init__, __repr__, __eq__ |
| Pydantic | API validation, FastAPI | Validation, serialization, aliases |
| TypedDict | Type checking only | Dict compatibility, no overhead |
Options Explained
snake_case conversion
Converts camelCase JSON keys to Python's preferred snake_case:
// JSON: "firstName"
# Python: first_nameFor Pydantic, this adds Field(alias="...") to map between formats.
Optional for nulls
When enabled, null values become Optional[T] with a default of None.
Type Mapping
| JSON | Python |
|---|---|
| string | str |
| integer | int |
| float | float |
| boolean | bool |
| null | Optional[T] or None |
| array | List[T] |
| object | Nested class |
Using Generated Classes
With dataclass
import json
# Parse JSON
data = json.loads(json_string)
user = User(**data)
# Access fields
print(user.user_name)
# Convert back to dict
from dataclasses import asdict
user_dict = asdict(user)With Pydantic
# Parse JSON directly
user = User.model_validate_json(json_string)
# Or from dict
user = User(**data)
# Convert to JSON
json_output = user.model_dump_json()
# With aliases (original JSON keys)
json_output = user.model_dump_json(by_alias=True)Working with APIs
For comprehensive JSON handling in Python, see our Python JSON Guide which covers parsing, serialization, and common patterns.
Related Tools
- Python JSON Guide — Complete guide to JSON in Python
- JSON Validator — Validate JSON before converting
- JSON to Java — Generate Java POJOs
- JSON to TypeScript — Generate TS interfaces
- JSON Schema Generator — Create schema from data
Frequently Asked Questions
Should I use dataclass or Pydantic?
Use dataclass for simple data containers without validation needs. Use Pydantic when you need validation, serialization, or are building APIs with FastAPI. Pydantic adds ~2ms overhead per parse but catches errors early.
What Python version do I need?
Dataclasses require Python 3.7+. Pydantic v2 requires Python 3.8+. TypedDict requires Python 3.8+ (or typing_extensions for 3.7).
How do I handle nested objects?
The converter automatically generates separate classes for nested objects and uses them as type annotations. Dependencies are ordered correctly in the output.