JSON to Python Dataclass

Generate Python dataclass definitions from JSON. Supports nested objects, arrays, Optional types, frozen, slots, and from_dict() method generation.

Input data is processed in your browser
Data is never sent to a server

Tool

Root class name
Indent:

or drag & drop a file

What is the JSON to Python Dataclass Converter?

This tool generates Python @dataclass definitions from JSON — perfect for quickly typing API responses, configuration files, or any JSON data structure.

Nested objects are expanded into separate named dataclasses, arrays are typed as List[T], and all classes are output in dependency order. The generated file includes from __future__ import annotations for forward references, and supports Optional types, frozen, slots, and from_dict() helpers for real-world use.

How to Use

  1. Paste your JSON into the left panel. Python dataclass definitions are generated instantly on the right. Click "Sample" to see an example.
  2. Enter a class name in the "Root class name" field (default: Root).
  3. Enable "Represent None as Optional[T]" to type null fields as `Optional[T] = None`.
  4. Enable "frozen=True" to generate `@dataclass(frozen=True)` for immutable instances.
  5. Enable "slots=True" to generate `@dataclass(slots=True)` for memory-efficient classes (requires Python 3.10+).
  6. Enable "Generate from_dict() method" to add a classmethod that constructs an instance from a plain dictionary.

FAQ

How are nested objects handled?

Each nested object is converted into its own dataclass and referenced using forward references (string annotations). The generated file includes `from __future__ import annotations` so it works out of the box.

How are field names converted?

JSON keys are automatically converted to Python snake_case. For example, `firstName` becomes `first_name`.

How are arrays handled?

Arrays are typed as `List[T]`. For arrays of objects, all keys across all elements are merged into a single dataclass and output as `List[ClassName]`.

How are null fields handled?

Null fields are generated with a default value of `None`. Due to Python's dataclass rules, fields with defaults are always placed after fields without defaults.