JSON to Python Converter

Generate Python classes from JSON data. Supports dataclasses, Pydantic, and TypedDict:

Loading...
Loading...

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] = None

Pydantic

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 = True

TypedDict

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

FormatBest ForFeatures
dataclassGeneral purpose, no depsAuto __init__, __repr__, __eq__
PydanticAPI validation, FastAPIValidation, serialization, aliases
TypedDictType checking onlyDict compatibility, no overhead

Options Explained

snake_case conversion

Converts camelCase JSON keys to Python's preferred snake_case:

// JSON: "firstName"
# Python: first_name

For 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

JSONPython
stringstr
integerint
floatfloat
booleanbool
nullOptional[T] or None
arrayList[T]
objectNested 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

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.