JSON to Python Dataclass Converter
Paste JSON to generate Python dataclasses, TypedDict, or Pydantic models. 100% client-side.
How to Convert JSON to Python Dataclasses
- Paste your JSON into the input area on the left.
- Choose a mode — Dataclass (default), TypedDict, or Pydantic.
- Copy or download the generated Python code using the buttons above the output.
- Add it to your project — import the class and use it to type your API responses or configuration data.
Python Type Mapping from JSON
This converter maps JSON types to their Python equivalents using the typing module for compatibility with Python 3.8+. The generated code includes from __future__ import annotations for forward reference support, meaning you can reference class names before they are defined in the same file.
Type Mapping Reference
- JSON string →
str - JSON integer →
int - JSON float →
float - JSON boolean →
bool - JSON null →
Optional[Any] - JSON array of strings →
List[str] - JSON array of objects →
List[ChildClass] - JSON nested object → separate class definition
Output Modes Explained
- Dataclass — Generates
@dataclassdecorated classes from thedataclassesmodule. Fields are annotated with Python type hints and have sensible defaults (field(default_factory=list)for lists,Nonefor optional values). Available in Python 3.7+. - TypedDict — Generates
TypedDictsubclasses for typing dictionaries. Unlike dataclasses, TypedDict instances are plain dicts at runtime — the typing is only for static analysis tools like mypy and pyright. Use TypedDict when you need to type existing dict-based code without changing its runtime behaviour. - Pydantic — Generates
BaseModelsubclasses from the Pydantic library. Pydantic provides runtime validation, type coercion, JSON serialization (.model_dump(),.model_dump_json()), and JSON schema generation. Requirespip install pydantic. Ideal for FastAPI, data pipelines, and anywhere you need validated data.
Working with dataclasses in Python
Python dataclasses, introduced in Python 3.7 via PEP 557, eliminate boilerplate by auto-generating __init__, __repr__, and __eq__ from field declarations. To load JSON into a dataclass: import json; from dataclasses import dataclass; data = json.loads(response_text); obj = MyClass(**data). For nested objects, you will need to manually convert nested dicts — consider using a library like dacite (pip install dacite) which handles nested dataclass instantiation automatically.
Pydantic vs. Dataclasses
Pydantic BaseModel provides several advantages over plain dataclasses for API data: automatic type coercion (a string "42" becomes int 42 if the field is typed as int), built-in validation with descriptive error messages, JSON serialization without extra libraries, and JSON Schema export for OpenAPI documentation. FastAPI, the popular Python web framework, is built on Pydantic and uses BaseModel for request and response models. If you are building a FastAPI service, the Pydantic mode output is ready to paste directly as your request/response schemas.
Naming Conventions
The converter uses PascalCase (UpperCamelCase) for class names, following Python's PEP 8 convention for class names. Field names use the original JSON key verbatim since Python field names in dataclasses and TypedDicts map directly to dictionary keys. If a JSON key contains characters that are invalid in Python identifiers (like hyphens or spaces), the converter replaces them with underscores. For Pydantic, you can add a model_config = ConfigDict(populate_by_name=True) to accept both the original and aliased field names.
Integrating with Existing Tools
The generated Python classes work well with popular tools in the Python ecosystem. For JSON serialization/deserialization with dataclasses, use dataclasses-json or dacite. For database mapping, combine with SQLAlchemy models. For schema validation, combine with jsonschema library. For async APIs, all modes are compatible with aiohttp and httpx. Once you have your Python types, you may also want our JSON Formatter to validate your input data before converting.