JSON to Python

Skip to editor
Explorer
Open Files

No files open

0 files open

jsoneditor.io v2

{
  "orderId": "ord_8F2K1",
  "status": "shipped",
  "customer": {
    "id": 4182,
    "name": "Ada Lovelace",
    "email": "ada@example.com"
  },
  "items": [
    {
      "sku": "KB-104",
      "name": "Mechanical keyboard",
      "qty": 1,
      "price": 89.99
    },
    {
      "sku": "MS-220",
      "name": "Wireless mouse",
      "qty": 2,
      "price": 24.5
    }
  ],
  "total": 138.99,
  "paid": true,
  "notes": null
}
JSONUTF-8
Spaces: 2

How do I convert JSON to a Python class?

To generate Python classes from JSON, paste a sample above, open Convert and press Python. You get Pydantic v2 models in a new tab, with modern type hints like list[Item] and str | None.

Attribute names are converted to snake_case and keep the original key as an alias, so orderId becomes order_id and still reads and writes "orderId". Nested objects become their own models, declared before the models that use them, and keys missing from some items default to None.

  1. Paste a sample JSON response into the editor.
  2. Open Convert and press Python.
  3. Copy the models into your project.

Example

JSON
{
  "orderId": "ord_8F2K1",
  "status": "shipped",
  "customer": {
    "id": 4182,
    "name": "Ada Lovelace",
    "email": "ada@example.com"
  },
  "items": [
    {
      "sku": "KB-104",
      "name": "Mechanical keyboard",
      "qty": 1,
      "price": 89.99
    },
    {
      "sku": "MS-220",
      "name": "Wireless mouse",
      "qty": 2,
      "price": 24.5
    }
  ],
  "total": 138.99,
  "paid": true,
  "notes": null
}
Python
from typing import Any

from pydantic import BaseModel, ConfigDict, Field


class Customer(BaseModel):
    id: int
    name: str
    email: str


class Item(BaseModel):
    sku: str
    name: str
    qty: int
    price: float


class Root(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    order_id: str = Field(alias="orderId")
    status: str
    customer: Customer
    items: list[Item]
    total: float
    paid: bool
    notes: Any

Questions people ask

How accurate is the generated Python?

As accurate as your sample. The generator only knows what it can see, so a field that can be null but never is in your sample won't be marked nullable. Paste a response with a few varied items for the best result.

How do I load JSON into the generated model?

Call Root.model_validate_json(text) on a JSON string, or Root.model_validate(data) on a dict you already parsed. model_dump(by_alias=True) writes the original keys back out.

Why Pydantic and not dataclasses?

Pydantic validates the data and maps keys with aliases, so names like orderId or shipping address work without extra code. Dataclasses would need a hand written loader for both.

Is my data uploaded anywhere?

No, this runs entirely in your browser, so nothing you paste is sent to a server. The only feature that sends data anywhere is the optional AI assistant, and only when you actually ask it something.