JSON to Python Class Converter

Generate typed Python classes from JSON, with from_dict and to_dict methods for parsing and serializing.

Generate typed Python classes from JSON

json.loads gives you nested dicts, so typos in keys only show up at runtime and your editor cannot autocomplete anything. This converter produces a class for each object in your JSON, with type-annotated attributes (str, int, float, bool, List[...]), a from_dict static method that checks each value's type while parsing, and a to_dict method for turning the object back into a dict for JSON. It uses only the typing module from the standard library.

How to use

  1. Paste a JSON sample, for example a webhook payload or an API response.
  2. Name the root class, for example Order.
  3. Save the code as a module and parse data with Order.from_dict(json.loads(text)); call to_dict() before json.dumps when sending it back.

Frequently asked questions

Are these dataclasses or Pydantic models?
Neither. They are plain classes with type hints and explicit from_dict/to_dict methods, so they need no third-party packages. You can add @dataclass or port the fields to Pydantic if your project already uses it.
What happens if the data does not match the types?
The generated helper functions use assert isinstance checks, so from_dict raises an AssertionError when a value has an unexpected type. Note that asserts are skipped when Python runs with -O.
Which Python version is required?
The code uses annotations from the typing module and works on Python 3.7 and later.

Example: JSON to Python

Input JSON

{
  "order_id": "A-1001",
  "total": 59.9,
  "paid": true,
  "items": [{ "sku": "BOOK-1", "quantity": 2 }]
}

Generated Python (Order)

from typing import Any, List, TypeVar, Callable, Type, cast


T = TypeVar("T")


def from_str(x: Any) -> str:
    assert isinstance(x, str)
    return x


def from_int(x: Any) -> int:
    assert isinstance(x, int) and not isinstance(x, bool)
    return x


def from_float(x: Any) -> float:
    assert isinstance(x, (float, int)) and not isinstance(x, bool)
    return float(x)


def from_bool(x: Any) -> bool:
    assert isinstance(x, bool)
    return x


def from_list(f: Callable[[Any], T], x: Any) -> List[T]:
    assert isinstance(x, list)
    return [f(y) for y in x]


def to_float(x: Any) -> float:
    assert isinstance(x, (int, float))
    return x


def to_class(c: Type[T], x: Any) -> dict:
    assert isinstance(x, c)
    return cast(Any, x).to_dict()


class Item:
    sku: str
    quantity: int

    def __init__(self, sku: str, quantity: int) -> None:
        self.sku = sku
        self.quantity = quantity

    @staticmethod
    def from_dict(obj: Any) -> 'Item':
        assert isinstance(obj, dict)
        sku = from_str(obj.get("sku"))
        quantity = from_int(obj.get("quantity"))
        return Item(sku, quantity)

    def to_dict(self) -> dict:
        result: dict = {}
        result["sku"] = from_str(self.sku)
        result["quantity"] = from_int(self.quantity)
        return result


class Order:
    order_id: str
    total: float
    paid: bool
    items: List[Item]

    def __init__(self, order_id: str, total: float, paid: bool, items: List[Item]) -> None:
        self.order_id = order_id
        self.total = total
        self.paid = paid
        self.items = items

    @staticmethod
    def from_dict(obj: Any) -> 'Order':
        assert isinstance(obj, dict)
        order_id = from_str(obj.get("order_id"))
        total = from_float(obj.get("total"))
        paid = from_bool(obj.get("paid"))
        items = from_list(Item.from_dict, obj.get("items"))
        return Order(order_id, total, paid, items)

    def to_dict(self) -> dict:
        result: dict = {}
        result["order_id"] = from_str(self.order_id)
        result["total"] = to_float(self.total)
        result["paid"] = from_bool(self.paid)
        result["items"] = from_list(lambda x: to_class(Item, x), self.items)
        return result


def order_from_dict(s: Any) -> Order:
    return Order.from_dict(s)


def order_to_dict(x: Order) -> Any:
    return to_class(Order, x)

Convert JSON to other languages