JSON to Python

Paste JSON to generate Python dataclasses or TypedDict with full nested type inference.

About This Tool

The JSON to Python converter is a free browser-based tool that automatically generates Python dataclasses or TypedDict definitions from any JSON data. Instead of manually writing class definitions for API responses, configuration files, or database records, you can paste the raw JSON and get accurate, ready-to-use Python code in milliseconds.

The converter performs recursive type inference across your entire JSON structure. Primitive values are mapped to their corresponding Python types — str, int, float, bool, and None. Nested objects are extracted into their own named classes, with names derived from the field key using PascalCase conversion. For example, a field called billing_address produces a BillingAddress class. Arrays are analyzed to determine a consistent element type; if the array contains mixed types, the field is typed as List[Any]. Empty arrays also default to List[Any] since the element type cannot be determined from the data alone.

All processing happens entirely in your browser. Your JSON data never leaves your machine — there are no server round-trips, no logging, and no third-party analytics on your input. This makes it safe for sensitive payloads such as internal API responses, authentication tokens, and configuration files that contain secrets.

You can choose between two output modes. The dataclass mode generates classes decorated with @dataclass from Python's standard library, complete with type annotations and optional from_dict / to_dict methods for serialization. The TypedDict mode produces lightweight type definitions that describe dictionary shapes for static analysis tools like mypy and Pyright. Additional options let you wrap all fields in Optional[T] for partial data, and the root class name is configurable so the generated code integrates directly into your codebase without renaming.

How to Use

  1. Paste or type your JSON into the JSON Input panel on the left.
  2. The Python output updates automatically in the right panel as you type.
  3. Set the Root class name to customize the top-level class name (defaults to "Root").
  4. Switch between dataclass and TypedDict output modes using the toggle buttons.
  5. Enable Optional to wrap all fields in Optional[T], or enable from_dict / to_dict to include serialization methods on dataclasses.
  6. Click Copy or press Ctrl+Shift+C to copy the generated Python code to your clipboard. Use Sample to load example JSON.

Popular JSON to Python Examples

View all JSON to Python examples →

FAQ

What is the difference between dataclass and TypedDict output?

Python dataclasses are full classes with __init__, __repr__, __eq__, and other dunder methods generated automatically by the @dataclass decorator. They are ideal when you need instances with behavior, validation, or methods like from_dict/to_dict. TypedDict, on the other hand, is a typing construct that describes the shape of a dictionary. It does not create a new class at runtime \u2014 it is used purely for static type checking. Choose dataclass when you need real objects and TypedDict when you want to annotate existing dict data.

How does the tool handle nested JSON objects?

Each nested JSON object is extracted into its own named Python class. The class name is derived from the field key using PascalCase conversion. For example, a field called shipping_address produces a ShippingAddress class. If a name collision occurs (e.g. two fields that both normalize to the same name), a numeric suffix is appended automatically (Address, Address2, etc.). This works for arbitrarily deep nesting.

How are null values handled?

JSON null values are mapped to Optional[None] by default, which simplifies to Optional[Any] since the actual type cannot be inferred from null alone. When the Optional fields toggle is enabled, all fields across all classes are wrapped in Optional[T], making them explicitly nullable. This is useful for partial data or API responses where fields may be missing.

What happens with arrays of mixed types?

The converter inspects every element in a JSON array and collects all distinct Python types. If all elements share the same type, a simple List[T] annotation is produced (e.g. List[str]). If multiple types are found (e.g. strings and numbers mixed together), the field is typed as List[Any]. Empty arrays also default to List[Any] since the element type cannot be determined.

Is my data safe?

Yes. All type inference and code generation runs entirely in your browser using JavaScript. No data is sent to any server. You can verify this by checking the Network tab in your browser's developer tools while using the tool.

What are the from_dict and to_dict methods?

When generating dataclasses, the tool can optionally include from_dict (a @classmethod that constructs an instance from a plain dictionary) and to_dict (an instance method that serializes the dataclass back to a dictionary). These methods handle nested objects and lists recursively, making it easy to convert between JSON-compatible dicts and typed Python objects without external libraries.

Does the generated code follow PEP 8?

Yes. The generated Python code follows PEP 8 conventions: class names use PascalCase, field names use snake_case (preserved from the JSON keys), imports are grouped at the top of the file, and proper spacing is maintained between class definitions. The code is ready to paste directly into your project.

Related Tools