JSON to Python Dataclass Converter

Paste JSON to generate Python dataclasses, TypedDict, or Pydantic models. 100% client-side.

JSON Input
Python Output
Paste JSON above to generate Python classes.

How to Convert JSON to Python Dataclasses

  1. Paste your JSON into the input area on the left.
  2. Choose a mode — Dataclass (default), TypedDict, or Pydantic.
  3. Copy or download the generated Python code using the buttons above the output.
  4. 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 @dataclass decorated classes from the dataclasses module. Fields are annotated with Python type hints and have sensible defaults (field(default_factory=list) for lists, None for optional values). Available in Python 3.7+.
  • TypedDict — Generates TypedDict subclasses 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 BaseModel subclasses from the Pydantic library. Pydantic provides runtime validation, type coercion, JSON serialization (.model_dump(), .model_dump_json()), and JSON schema generation. Requires pip 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.

Frequently Asked Questions

A Python dataclass is a class decorated with @dataclass from the dataclasses module (Python 3.7+). The decorator auto-generates __init__, __repr__, and __eq__ methods based on the class's type-annotated fields. Dataclasses are ideal for modelling structured data like JSON responses because they provide both type hints and automatic boilerplate code generation.
Python dataclasses create actual class instances with methods. TypedDict creates dictionary type hints for static analysis only — at runtime a TypedDict is just a plain dict. Pydantic BaseModel provides runtime validation, type coercion, JSON serialization, and schema generation. For API models with validation, use Pydantic. For simple data containers, use dataclasses. For typing existing dicts, use TypedDict.
No. This converter runs entirely in your browser using JavaScript. Your JSON never leaves your device. There is no server, no logging, and no data collection.
JSON null values are mapped to Python's Optional[type] or simply None as a default. In the generated code, a null field is typed as Optional[Any] and defaults to None. In Pydantic mode, Optional fields automatically accept None as a valid value.
Dataclass output requires Python 3.7+. TypedDict output requires Python 3.8+ (or typing_extensions for 3.7). Pydantic output requires pydantic v2 (pip install pydantic). All generated code uses from __future__ import annotations for forward reference support, making it compatible with Python 3.8 and above.