JSON to Python dataclass

Convert JSON to Python dataclasses: real output, renamed keys, optional fields and loading nested data.

loading JSON to Python dataclass
About this tool

JSON to Python dataclass: from JSON to code

Paste JSON and get Python dataclasses with type hints. Keys that are not valid Python names are turned into snake_case with a comment that remembers the original key, nested objects become their own classes (written before the class that uses them), optional fields get a default of None and use the X | None syntax with from __future__ import annotations.

This page opens the converter on Python. Paste your JSON, choose the root name and the flavour, and copy the result; everything is generated in your browser and the JSON is never sent anywhere.

A real conversion

The tool opens with this JSON, an order with a customer and two items:

{
  "id": 48213,
  "status": "paid",
  "paid_at": "2026-11-21T14:03:00Z",
  "total": 389.3,
  "coupon": null,
  "customer": { "name": "Ana Souza", "email": "ana@example.com", "vip": true },
  "items": [
    { "sku": "KB-01", "qty": 2, "price": 149.9, "tags": ["usb", "keyboard"] },
    { "sku": "MS-07", "qty": 1, "price": 89.5 }
  ]
}

and this is what it writes for Python:

from __future__ import annotations

from dataclasses import dataclass
from typing import Any


@dataclass
class Customer:
    name: str
    email: str
    vip: bool


@dataclass
class Item:
    sku: str
    qty: int
    price: float
    tags: list[str] | None = None


@dataclass
class Root:
    id: int
    status: str
    paid_at: str
    total: float
    coupon: Any
    customer: Customer
    items: list[Item]

How each JSON value is typed

In the JSONIn Python
stringstr
integerint
decimalfloat
true / falsebool
nullAny
array of itemslist[Item]
field missing in some itemslist[str] or None

The second item of the sample has no "tags" key, and the first has one: that is why the tool treats it as optional.

Awkward keys, null and empty lists

{"user_id": 7, "display name": "Ana", "tags": [], "address": null, "scores": [9.5, 8], "meta": {"2fa": true}, "mixed": [1, "a"]}
from __future__ import annotations

from dataclasses import dataclass
from typing import Any


@dataclass
class Meta:
    f2fa: bool  # JSON key "2fa"


@dataclass
class Account:
    user_id: int
    display_name: str  # JSON key "display name"
    tags: list[Any]
    address: Any
    scores: list[float]
    meta: Meta
    mixed: list[Any]

"display name" becomes display_name with a comment holding the original key, "2fa" becomes f2fa (an identifier cannot start with a digit), and null and the empty list become Any and list[Any]. Remember to map the renamed keys yourself when you load the JSON.

The other flavour

The same small JSON, {"id": 1, "paid_at": "2026-11-21", "coupon": null}, in the default flavour and in the "typing" one:

from __future__ import annotations

from dataclasses import dataclass
from typing import Any


@dataclass
class Order:
    id: int
    paid_at: str
    coupon: Any
from dataclasses import dataclass
from typing import Optional, Any


@dataclass
class Order:
    id: int
    paid_at: str
    coupon: Any

The typing flavour writes List and Optional from the typing module, for Python before 3.9 and 3.10, which do not accept list[str] | None.

Filling the dataclasses from the JSON

import json

data = json.loads(text)
customer = Customer(**data["customer"])
items = [Item(**item) for item in data["items"]]
order = Root(**{**data, "customer": customer, "items": items})

A dataclass does not convert nested dicts by itself: you build the inner objects first, as above, or use a library such as dacite or Pydantic that does it for you. If you want validation as well, the Pydantic page of this tool writes models instead.

Common mistakes

  • Passing the raw dict to a dataclass and getting a nested dict instead of an object: dataclasses do not convert recursively.
  • Believing the hints are checked: Python does not enforce them at runtime, so id: int accepts "x" without complaint. Use Pydantic when you need that.

How to use the converter

  1. Paste your JSON on the left (or keep the example); a mistake is reported with its line and column.
  2. Set the root name and the flavour; the code updates as you type.
  3. Copy the code or download it as a file.

Frequently asked questions

How do I convert JSON to a Python class?

Paste the JSON and copy the dataclasses; load the data with json.loads and build the objects from the dicts.

Dataclass or Pydantic model: which one?

Dataclass for plain containers with no extra dependency; Pydantic when the data comes from outside and must be validated and converted automatically.

Why does the file start with from __future__ import annotations?

It lets the classes refer to each other and use the X | None syntax in annotations on older Python versions, because annotations are then read as text instead of evaluated.

Report a problem