FormatterHQ

JSON to Python Dataclass Generator

Generate Python dataclasses from a JSON sample, with type hints, Optional fields, nested classes and optional from_dict / to_dict conversion code.

Input JSON
Output PYTHON
Ready.

How to use the JSON to Python

  1. Paste your JSON sample (an object, or an array of objects) into the Input editor, or use Sample, Upload file or Load URL, or press Sample to try one.
  2. Set the Top-level class name, for example Task; it is converted to PascalCase. Nested types are named after their property keys.
  3. Tick Types only (no helpers) for bare dataclasses, or leave it off to include from_dict / to_dict methods with validation.
  4. Press Generate (it also re-runs automatically as you edit), then use Copy or Download.

JSON to Python: generate dataclasses

Working with json.loads output means dictionary lookups like data["owner"]["name"], no autocompletion and no type checking. This JSON to Python generator turns a sample into @dataclass classes with type hints, so editors and type checkers such as mypy or Pyright understand your data. Nested objects become their own classes and are defined before the classes that use them.

Example

{
  "id": 42,
  "title": "Write docs",
  "done": false,
  "priority": 2.5,
  "tags": ["docs"],
  "owner": { "name": "Ada", "email": null },
  "due": "2026-10-01T09:00:00Z",
  "subtasks": [
    { "title": "Outline", "estimate": 2 },
    { "title": "Draft" }
  ]
}

With Types only checked (class name Task):

from dataclasses import dataclass
from typing import Optional, List
from datetime import datetime


@dataclass
class Owner:
    name: str
    email: None = None


@dataclass
class Subtask:
    title: str
    estimate: Optional[int] = None


@dataclass
class Task:
    id: int
    title: str
    done: bool
    priority: float
    tags: List[str]
    owner: Owner
    due: datetime
    subtasks: List[Subtask]

Optional fields get a default of None and appear after required ones, as dataclasses require. Type hints use typing.List and Optional, which work on Python 3.7 and later; on 3.10+ you can modernize them to list[str] and int | None.

With conversion helpers

Dataclasses do not parse nested dictionaries by themselves: Task(**data) would leave owner as a plain dict and due as a string. With Types only off, each class gets a from_dict static method and a to_dict method, plus top-level functions task_from_dict and task_to_dict. They build nested objects, parse dates with dateutil, and assert that each value has the expected type, so malformed data fails loudly:

import json
task = task_from_dict(json.loads(text))
print(task.owner.name, task.due.year)

The helpers need the python-dateutil package when the data contains dates.

Naming

Strings that look like UUIDs are typed as uuid.UUID. JSON keys in camelCase are converted to snake_case attribute names following PEP 8 (isActive becomes is_active), and names that clash with Python keywords or builtins are adjusted (a key class becomes root_class, 2fa becomes the_2_fa). The helper methods map attributes back to the original JSON keys, which is a good reason to keep them when your keys are not already snake_case.

Type mapping

JSONPython
integer / decimalint / float
string / ISO datestr / datetime
booleanbool
arrayList[T]
objecta dataclass
missing or nullOptional[T] = None

Top-level arrays

If your JSON is a list of records, the generator creates a class for the element, named after the class name with an Element suffix (for example TaskElement), and the helper functions return a List of them. Pasting a list is also the best way to get accurate types, because every record contributes: a key missing from any record becomes Optional, and a field that is an integer in one record and a decimal in another becomes float.

Alternatives

If you use Pydantic, the generated dataclasses are a good starting point: change @dataclass to subclasses of BaseModel and Pydantic will parse nested data and dates itself. For TypedDict-based typing, the same field list applies. Format the output with the Python Formatter, or check the input with the JSON Validator. Converting from XML? Use XML to Python. Generation runs in your browser.

Frequently asked questions

Which Python version does the output target?
The code uses dataclasses and typing.List / Optional, which work on Python 3.7 and newer. On 3.10+ you can switch to list[str] and int | None syntax.
Why not just use Task(**json_data)?
Keyword unpacking does not convert nested dictionaries into objects or strings into datetimes. The generated from_dict helpers do both and validate types.
Can I generate Pydantic models instead?
The tool generates dataclasses. Converting them to Pydantic models is usually a matter of inheriting from BaseModel and removing the @dataclass decorator.
Why were some property names changed?
Names are converted to snake_case and adjusted when they are Python keywords or builtins. The helper methods keep the mapping to the original JSON keys.

Last updated September 26, 2026

Related tools