JSON to Python Converter Online — Generate Dataclasses

Generate typed Python @dataclass classes from any JSON sample — int, str, float, Optional, List, and nested classes. 100% in your browser.

What is a JSON to Python converter?

A JSON to Python converter reads a JSON sample and emits typed @dataclass classes with the right field names and PEP 484 type hints — int, str, float, bool, Optional[X], List[X], and nested dataclasses. The output is stdlib-only Python that drops into FastAPI, Flask, Django, Pydantic projects, or scripts.

Modelling a 50-field webhook by hand wastes time and misses edge cases. OpenFormatter's converter walks the JSON, infers types, and emits idiomatic Python ready for json.loads(...), dacite.from_dict, or a one-line swap to Pydantic BaseModel. Everything runs in your browser — no third-party server sees the JSON.

How to generate a Python dataclass from JSON — 4 steps

  1. Paste a representative JSON. Use a real response. Booleans must be true/false; integers should not be quoted, otherwise they infer as str.
  2. Click Convert. The tool emits a Root dataclass plus one dataclass per nested object.
  3. Copy into a .py module. The output is one file with all dataclasses; the imports are already at the top.
  4. Deserialize. Flat dicts: Root(**json.loads(payload)). Nested: from dacite import from_dict; from_dict(Root, json.loads(payload)).

JSON to Python type mapping

Sample JSON

{
  "id": 42,
  "username": "ada",
  "is_admin": true,
  "balance": 1234.56,
  "profile": { "country": "UK" },
  "roles": ["admin", "engineer"]
}

Generated Python @dataclass

from dataclasses import dataclass
from typing import Any, List, Optional

@dataclass
class Profile:
    country: str

@dataclass
class Root:
    id: int
    username: str
    is_admin: bool
    balance: float
    profile: Profile
    roles: List[str]

Stdlib dataclasses

Pure stdlib output — no third-party dependencies. Works on any Python 3.7+ install, including AWS Lambda runtimes.

Nested dataclasses

Every nested JSON object becomes its own @dataclass with PascalCase name, referenced by the parent.

Client-Side Only

JSON is parsed in your browser. Real customer data, tokens, or internal API contracts never leave the device.

Common use cases

  • check_circleGenerating FastAPI Pydantic request and response models from a frontend JSON sample
  • check_circleBuilding Django Rest Framework serializer-aligned dataclasses for type-safe internal logic
  • check_circleModelling Stripe, GitHub, Slack webhook payloads with typed dataclasses for strong autocompletion
  • check_circleProducing Pyright / mypy-friendly JSON shapes for an existing dict-heavy codebase
  • check_circleGenerating dataclasses for AWS Lambda event payloads (S3, API Gateway, SNS, SQS)
  • check_circleCreating typed configuration loaders that parse a config.json into Config(**data)
  • check_circleEmitting dataclasses as a starting point for SQLAlchemy 2.x mapped classes
  • check_circleModelling JSON files in data-engineering pipelines (Airflow, Prefect, Dagster)

Why client-side generation matters

Internal API contracts, customer payloads, signed payment URLs, and secrets often appear in the JSON you want to model. Pasting that into a server-side converter sends it across the wire — a non-starter for many enterprises. OpenFormatter generates the Python entirely in JavaScript on your machine. Open DevTools → Network and you will see no requests when you click Convert.

More Python & JSON tools

Format JSON, parse JSON in Python, or generate models for other languages — all browser-side.

Frequently Asked Questions

Does it use dataclasses or Pydantic?

The default output is a stdlib @dataclass class, which has zero third-party dependencies and works on any Python 3.7+ install. To convert to Pydantic, replace @dataclass with class Root(BaseModel): and add `from pydantic import BaseModel`. Pydantic accepts the same type hints and adds runtime validation, JSON schema generation, and ORM-style construction.

How are Optional fields handled?

A field whose value is null in the sample becomes Optional[Any] (or Optional[Type] when a type is inferred). Pair it with a default of None — `field: Optional[int] = None` — so dataclass construction does not fail when the key is missing.

Can I generate a TypedDict instead?

TypedDicts are best for adding types to existing dict-shaped data without changing runtime behaviour. To convert the output, change @dataclass / class Root: to `class Root(TypedDict):` and remove the colons after each field. TypedDicts require Python 3.8+ for the class-based syntax.

How is JSON snake_case preserved?

JSON keys are kept verbatim as Python attribute names — `is_admin` stays `is_admin`, `userId` stays `userId`. Python convention prefers snake_case, but renaming attributes breaks JSON parsing unless you add a `json` library shim or use Pydantic with `Field(alias=...)`.

How are nested JSON objects modelled?

Each nested object becomes a separate @dataclass, named in PascalCase from the JSON key. The parent class references the child by type. For deserialization with stdlib only, write a helper that walks the dict and constructs each dataclass; or use the dacite library which does it generically.

Can I use this with FastAPI?

Yes — FastAPI uses Pydantic. Generate the dataclass output, then change @dataclass to class Root(BaseModel): and FastAPI will auto-validate request bodies, generate the OpenAPI schema, and produce a Swagger UI for you. No other changes are needed.

Is the JSON uploaded to your servers?

No. The conversion runs entirely in your browser via JavaScript. Open DevTools → Network and click Convert — there are no requests. JSON containing customer data, tokens, or proprietary schemas never leaves the device.

How do I deserialize JSON into the generated dataclass?

For a flat dataclass: `Root(**json.loads(payload))`. For nested classes use the dacite library: `from_dict(data_class=Root, data=json.loads(payload))` which recursively constructs every nested dataclass.

JSON to Python Converter Online — Generate Dataclasses