>_devtools

JSON to Python Class Converter

Generate Python dataclasses or Pydantic models with type hints from a JSON sample.

What this does

Generates typed Python classes from a JSON sample: @dataclass classes by default, or Pydantic BaseModel classes if you tick that option. Nested objects become their own classes, declared before they are used.

Example

Input

{
  "id": 42,
  "name": "Ada",
  "verified": true,
  "tags": [
    "admin",
    "dev"
  ],
  "orders": [
    {
      "sku": "A1",
      "qty": 2
    },
    {
      "sku": "B2",
      "qty": 1,
      "note": "gift"
    }
  ]
}

Output with default options

from dataclasses import dataclass

@dataclass
class Order:
    sku: str
    qty: int
    note: str | None = None

@dataclass
class Root:
    id: int
    name: str
    verified: bool
    tags: list[str]
    orders: list[Order]

How values are mapped

  • Whole numbers become int, other numbers float, strings str, booleans bool, arrays list[T].
  • Fields missing from some objects, or sometimes null, become T | None = None and are placed after required fields.
  • ISO 8601 timestamps become datetime and UUID strings become UUID when date detection is on.
  • Mixed-type arrays become list[int | str]. Pick an older Python version and the output switches to the matching typing imports.

Options

Choose the target Python version (3.5 through 3.10), snake_case property names, and dataclasses versus Pydantic. With Types only turned off, the output also includes conversion helpers that read and write the original JSON keys.

Things to know

The classes describe your sample, so include several objects when some keys are optional. Property names are converted to snake_case (so userId becomesuser_id); keep the helpers if you need the original JSON keys preserved.

Privacy

Generation runs in a Web Worker in your browser. The JSON is never uploaded.