JSON to Python Converter

Generate Python dataclasses from a JSON sample directly in your browser.

This tool runs entirely in your browser. Your files are never uploaded to a server.

from __future__ import annotations
from dataclasses import dataclass
from typing import Any, List

@dataclass
class Root:
    name: str
    age: int
    active: bool
    tags: List[str]
    address: Address


@dataclass
class Address:
    street: str
    zip: str

What this converter does

Reads a JSON value and generates matching Python dataclasses: one `@dataclass` per object shape found in the sample, with each field type-hinted from the value it holds.

How to use it

Paste a JSON object, array or primitive value into the editor. A `Root` dataclass (or a type alias, for a non-object root) appears immediately, along with one additional dataclass for every nested object shape.

Example

An object with a `name` string and a nested `address` object produces `class Root: name: str; address: Address` plus a separate `class Address` for the nested shape, named after the key it came from.

Type mapping

Strings become `str`, whole numbers become `int`, decimal numbers become `float`, booleans become `bool`, and arrays become `List[ElementType]`. JSON keys that aren't valid Python identifiers (containing a dash, for example) are converted to a safe name automatically.

Nested objects and lists

Each nested object gets its own dataclass, generated from its key. The output starts with `from __future__ import annotations`, so dataclasses can reference each other regardless of the order they're defined in.

Limitations

The generated dataclasses describe only the one JSON sample provided, and don't include a way to build one back from a dict (no `from_dict`/`to_dict` helpers) — you'll need to add serialization yourself if you need it. Arrays of objects are typed from their first item only.

Privacy

Parsing and code generation both run locally in your browser. The JSON you paste and the Python you generate are never uploaded.

Frequently Asked Questions

Why does the output include "from __future__ import annotations"?
It lets the generated dataclasses reference each other's types no matter which order they're defined in, without needing quoted forward references.
Does this handle JSON keys with dashes or spaces?
Yes — a key that isn't a valid Python identifier is converted into a safe one automatically (for example first-name becomes first_name), though it may not exactly resemble the original key.
Does this include JSON serialization/deserialization code?
No — it only generates the dataclass shapes; add your own json.loads/dataclasses.asdict or a library like dacite if you need to convert between JSON and these objects.