Skip to content
Sealpaste

Delete all recent work?

This removes the 0 items saved in this browser. Your settings stay.

Stop saving recent work?

New work won't be saved in this browser. The 0 items already saved can stay or go.

JSON to Python Class

JSON

Paste JSON, or

CtrlV paste · Ctrl↵ run now

Python
Ready
Ln 1, Col 1
Runs in your browser. Nothing you paste is uploaded unless you share it.

Turn a JSON sample into Python dataclasses, Pydantic v2 models or TypedDicts, with snake_case names, aliases and Optional fields

Paste an API response and get Python classes for it: a dataclass, a Pydantic model that validates, or a TypedDict for type checkers.

How to use it

  1. Paste a JSON sample, or open a .json file.
  2. Choose the style: dataclass, Pydantic or TypedDict, and set the root class’s name.
  3. Copy the classes or download models.py. Nested classes come first, so the file runs as it is.

Examples

A Pydantic model with an alias

Input
{"orderId": "A-17", "total": 42.5, "lines": [{"sku": "MUG-01", "qty": 2}]}
Result
from pydantic import BaseModel, Field

class Line(BaseModel):
    sku: str
    qty: int

class Root(BaseModel):
    order_id: str = Field(alias="orderId")
    total: float
    lines: list[Line]

Pydantic names the field order_id and reads "orderId" from the JSON. Root.model_validate(data) checks it.

A key that is a Python keyword

Input
{"name": "Ada", "email": null, "from": "uk"}
Result
from dataclasses import dataclass
from typing import Any

@dataclass
class Root:
    name: str
    email: Any
    from_: str  # "from" in the JSON

from can’t be a field name, so it is from_ with a comment naming the key.

The same as a TypedDict

Input
{"name": "Ada", "email": null, "from": "uk"}
Result
from typing import Any, TypedDict

Root = TypedDict(
    "Root",
    {
        "name": str,
        "email": Any,
        "from": str,
    },
)

A TypedDict keeps the JSON’s keys; one that isn’t a valid name uses this form.

How the types are read

The sample is read with a few rules, the same for every language:

  • Every object gets its own named type; the name comes from its key (customer → Customer, line_items → LineItem).
  • The objects in one array are merged into one type, so a field missing from some of them is marked optional.
  • A field that is null in one place and a string in another is a nullable string. A field that is only ever null, or mixes kinds such as 1 and "a", gets the language’s “anything” type.
  • Integers and decimals are told apart by how they are written: 25 is an integer, 25.0 and 12.5 are not. An integer past 2,147,483,647 gets a 64-bit type, and one past 64 bits a big-number type, read from the digits, never rounded.
  • Empty arrays get a list of “anything”, since the sample doesn’t say what they hold.

Which style to choose

All three use Python 3.10+ syntax (str | None, list[Item]); NotRequired needs 3.11.

  • dataclass: plain classes from the standard library. Field names keep the JSON’s keys where Python allows them, so Root(**data) works for flat objects; nested objects stay dicts unless you convert them.
  • Pydantic (v2): models that validate and convert nested objects for you. Fields get snake_case names with Field(alias=…) for the JSON key.
  • TypedDict: no class at runtime, just a type for mypy or Pyright over the dict json.loads gives you. Optional keys are NotRequired.

Optional and nullable fields

A field missing from some objects gets = None and moves after the required ones, since a dataclass can’t have a required field after one with a default. A field that can be null is typed str | None.

Questions

Does Pydantic write the JSON keys back?
It reads them by alias. To write them back the same way, call model_dump(by_alias=True) or model_dump_json(by_alias=True).
Why are the classes in reverse order?
A class has to be defined before another one uses it in Python, so nested classes come first and the root class last.
Is my JSON uploaded?
No. Everything runs in your browser, so your JSON stays on your device. Nothing you paste is uploaded unless you share a result, which is locked with your password first.

Related tools