Python Dataclass Generator

Generate Python dataclasses from a JSON document.

JSON Input
Python Output
Python dataclass output appears here

Related Tools

Documentation

What is Python Dataclass Generator?

This tool infers Python @dataclass definitions from a JSON sample, giving you typed, PEP 8-compliant classes you can drop straight into a project instead of working with raw dicts.

How it works

The JSON is parsed and walked recursively by value type. Each object becomes its own @dataclass, named from its key and PascalCased (authorAuthor), while its keys become fields converted from camelCase (or any other casing) to snake_case to match PEP 8 — so a JSON key like clientSide becomes the field client_side. Type inference maps strings to str, booleans to bool, integers to int, non-integer numbers to float, arrays to List[...] (typed from the first element), and null to Optional[Any]. Nested classes are emitted as separate top-level dataclasses — the root class first, then each nested one — all under a single from dataclasses import dataclass / from typing import Any, List, Optional header.

Features

  • Custom root class name (defaults to Root)
  • Automatic camelCase → snake_case field renaming to match PEP 8
  • Nested JSON objects become their own @dataclass, not a raw nested dict type
  • Correct List[...] and Optional[Any] typing from typing
  • Runs entirely client-side — nothing is uploaded
  • Load from file, copy, or download as a .py file

Example

Input: {"name":"DevFormats","version":"2.0","free":true,"price":null,"tags":["json","yaml"],"author":{"type":"web-tool","clientSide":true}}

Output (root name Root):

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


@dataclass
class Root:
    name: str
    version: str
    free: bool
    price: Optional[Any]
    tags: List[str]
    author: Author


@dataclass
class Author:
    type: str
    client_side: bool

Common errors / edge cases

Invalid JSON reports a line/column error from the browser's native JSON.parse. Because the root class is emitted before the nested classes it references (e.g. author: Author appears above the Author class definition), running the file as-is under Python without from __future__ import annotations will raise a NameError at class-definition time for older forward-reference-sensitive setups — modern Python evaluates dataclass field annotations lazily enough that this typically isn't an issue, but if you hit it, add that future import or reorder the classes.

Best practices

Add from __future__ import annotations at the top of the file if you see forward-reference errors, since it postpones evaluation of all annotations. For fields that are only sometimes present rather than always null, consider giving them an explicit default (e.g. = None) so the dataclass can be constructed without every field.

Frequently Asked Questions

Why are field names snake_cased?

It matches PEP 8, Python's standard naming convention, even when the source JSON uses camelCase keys.

How are null values handled?

A null value produces Optional[Any], since the sample didn't reveal the field's real type.

How are nested objects handled?

Each nested object gets its own @dataclass, derived from its key, rather than a nested dict type.

Is my data uploaded anywhere?

No — generation runs entirely in your browser.