JSON to Python converter

Paste a JSON response or config and get Python type definitions for it. Choose @dataclass for objects you create in code or TypedDict for typing plain dicts from json.loads. Nested objects become their own classes, and keys missing from some items become optional.

Generate

Runs in your browser: nothing you enter is sent anywhere.

How to use it

  1. Paste a JSON sample. Use a realistic one: the more items an array has, the better optional keys are detected.
  2. Choose @dataclass or TypedDict.
  3. Copy the code. Classes for nested objects come first, so the file runs from top to bottom.

How it works

The sample is read into a shape: each key of each object with every type seen there. All objects in one array are merged into a single class.

  • str, int, float, bool, list[…] and nested classes are written directly; null adds Optional[…].
  • Class names come from the key: “orders” holds a list of Order, and “billing_address” becomes BillingAddress. The top level is Root.
  • Imports include only what is used: dataclass, Any, Optional, TypedDict, NotRequired.

Worked example

{"items":[{"sku":"A","note":null,"qty":1},{"sku":"B","note":"x"}]} with @dataclass gives:

from dataclasses import dataclass
from typing import Optional


@dataclass
class Item:
    sku: str
    note: Optional[str]
    qty: Optional[int] = None


@dataclass
class Root:
    items: list[Item]

“note” is sometimes null, so it is Optional[str]. “qty” is missing from the second item, so it gets a default and moves to the end.

Tips

  • Check field types where the sample was lucky: a price of 10 in the sample becomes int though it may be 9.99 elsewhere.
  • Rename classes to match your domain after generating.
  • For JSON Schema or TypeScript, use the JSON schema generator or JSON to TypeScript.

Frequently asked questions

How are JSON types mapped to Python?

Strings become str, true and false bool, whole numbers int and other numbers float (a mix of both becomes float). Arrays become list[...], objects become classes, and null makes the type Optional[...]. A place holding different kinds of values becomes Any.

How are optional keys handled?

When an array holds several objects, a key missing from some of them is optional. In a dataclass it becomes Optional[...] = None and moves after the required fields, as dataclasses require; in a TypedDict it is wrapped in NotRequired[...].

What happens to keys that are not valid Python names?

In a dataclass, keys like first-name, class or 2fa become first_name, class_ and f_2fa, with the original JSON key in a comment. In a TypedDict the functional syntax TypedDict('Name', {...}) keeps the keys exactly.

Which Python version do I need?

The code uses list[...] and Optional from typing, which work in Python 3.9 and newer. NotRequired, used for optional TypedDict keys, needs Python 3.11 or typing_extensions.

Does a dataclass parse nested JSON for me?

No. Root(**data) fills the top level only; nested dicts stay dicts. Convert nested parts yourself or use a library such as pydantic or dacite.

More in Developer

All tools in this category: Developer ·Updated November 4, 2026

Privacy choices

Google Analytics shows us which tools are used and whether they work. It may set cookies. Your choice is kept in this browser.