formatjson.pro
Validate, pretty-print or minify JSON in your browser — nothing is uploaded.

JSON to Python Dataclass

Paste a sample JSON object below and generate Python <code>@dataclass</code> definitions with type hints. Each nested object becomes its own dataclass, and lists are typed with <code>List[...]</code>.

How the type mapping works

Each JSON value maps to a Python type hint: a whole number becomes int, a decimal becomes float, a string becomes str, true/false becomes bool, and null becomes Any. The tool emits the from dataclasses import dataclass and from typing import List, Any imports you need.

Lists and nested objects

A JSON array becomes List[T] where T is inferred from the first element, so ["admin","dev"] gives List[str]. A nested object becomes its own @dataclass — "address": {...} produces an Address dataclass referenced by the parent.

From dataclass to real parsing

A dataclass gives you typed attributes and a generated __init__, but it does not validate JSON on its own. For nested structures, construct child dataclasses explicitly, or switch to pydantic / dataclasses-json when you need parsing and validation from a dict.

FAQ

Does the dataclass parse JSON automatically?

No. A @dataclass defines typed fields and an __init__, but json.loads returns a plain dict. Build the dataclass from that dict yourself, or use pydantic / dataclasses-json for automatic parsing of nested objects.

Why is a field typed Any?

A null value carries no type information, so the tool falls back to Any. Replace it with the real type (often Optional[...]) once you know what non-null values look like.

How are nested objects handled?

Each nested object becomes its own @dataclass, and the parent field references it by name — mirroring the JSON structure.

Is my JSON sent to a server?

No. Everything runs in your browser in JavaScript; your JSON never leaves the page.