Boneyard Tools

JSON to Python Dataclass Generator

Paste a JSON sample and get typed Python dataclasses instantly. Nested objects become their own classes defined above the ones that use them, lists infer their element type, and null fields become Optional with a None default. Rename the root class and copy the result.

How to convert JSON to a Python dataclass

  1. Paste a JSON object into the input box.
  2. Optionally rename the root dataclass (defaults to Root).
  3. Copy the generated classes or download them as a .py file.

Examples

Flat object

{"name":"Ada","age":36,"active":true}
@dataclass
class Root:
    name: str
    age: int
    active: bool

Nested object and list

{"user":{"id":1},"tags":["a","b"]}
@dataclass
class User:
    id: int


@dataclass
class Root:
    user: User
    tags: List[str]

Frequently asked questions

What does this tool do?

It reads a JSON sample and generates matching Python dataclasses, inferring the type of every field and adding the dataclasses and typing imports so you can paste the result straight into your code.

How are nested objects and lists handled?

Each nested object becomes its own @dataclass, named in PascalCase from its key and defined before the class that references it. Lists infer their element type as List[T], a list of objects gets a generated class, and lists with mixed values become List[Union[...]].

How are JSON types mapped to Python?

Strings become str, whole numbers become int, decimals become float, and booleans become bool. A null value becomes Optional[Any] with a None default. JSON has no separate integer type, so a value like 36 maps to int while 1.5 maps to float.

Why are some fields reordered or renamed?

Keys are converted to snake_case, so firstName becomes first_name. Fields that default to None are listed after the required fields because Python forbids a non-default field after a default one, which keeps the generated dataclass valid.

Is my JSON sent to a server?

No. Parsing and code generation run entirely in your browser, so your data never leaves your machine.

Why did I get an error instead of a dataclass?

The tool needs valid JSON with an object at the top level. Invalid syntax or a top-level array, string or number returns an error message instead of classes.

Related tools