JSON to Python Converter

Data engineers materializing API responses into typed models, backend developers writing a Pydantic-adjacent dataclass by hand, and script writers who keep re-parsing the same nested JSON with bracket notation all share a common friction point. Transcribing a payload into a Python class is slow, error-prone, and easy to get subtly wrong on the first pass.

Paste your JSON and this converter emits a Python dataclass, TypedDict, or plain typed class for every nested object, with correct int, float, str, bool, and list annotations. It runs entirely in the tab, so sample payloads containing internal IDs or customer data never leave the machine.

 

How the conversion works

The converter walks the parsed JSON tree and maps each value to a Python type. Whole numbers become int, decimal numbers become float, true and false become bool, and null becomes None with an Optional annotation. Strings become str, arrays become list of the element type, and every distinct nested object becomes its own class so nesting is preserved instead of flattened.

Field names are converted to PascalCase and any Python keyword collision is escaped with a trailing underscore, so a field named class becomes class_. In dataclass mode a field whose sample value is null receives a None default so instantiating without it works. In TypedDict mode every field is required, which matches how mypy validates dict literals.

The generated code uses only the standard library. Copy it into a .py file and import it directly, no pip install required.

Before and after: a typical API response

Input, a JSON payload from a user endpoint:

{ "id": 42, "name": "Ada Lovelace", "active": true,
  "tags": ["math", "code"],
  "address": { "city": "London" } }

Output, dataclass style:

from __future__ import annotations
from dataclasses import dataclass, field
from typing import Optional

@dataclass
class Root:
    Id: int
    Name: str
    Active: bool
    Tags: list[str]
    Address: Optional[Address] = None

@dataclass
class Address:
    City: str

Who converts JSON to Python with this tool

Developers

  • Generate a dataclass for a REST response without hand-typing every field annotation.
  • Produce a TypedDict so mypy validates dict keys pulled from a third-party API.
  • Spin up typed models for a new FastAPI service and start compiling immediately.

Production

  • Regenerate models after a vendor changes their API and diff the output against the old class.
  • Enforce a consistent annotation style across a data engineering codebase.
  • Validate that Optional annotations match the real payload before tightening type checks.

Workflow

  • Convert the same JSON to Python and C# so backend and data teams share identical field names.
  • Paste the generated class into a scratch module, run it once, then move it into the real package.
  • Pair with the JSON validator to confirm the payload parses before generating the model.

Common mistakes when generating Python from JSON

  • Confusing TypedDict with a dataclass. A TypedDict does not create a real class at runtime, it only describes a dict shape for type checkers. If you try to instantiate it like a class or access attributes with dot notation, you get a runtime error. Use the dataclass style when you want objects, TypedDict when you want dict validation.
  • Assuming every number is an int. A field that looks like a small integer in one sample but carries decimals in another becomes float. If you know a field is always integral, switch the annotation to int by hand. Leaving it as float is safe for mypy but loses precision intent.
  • Deleting the trailing underscore on keyword fields. A JSON field named class or import becomes class_ or import_ in Python. Removing the underscore makes the file fail to compile. Keep the suffix or rename the field and adjust the mapping manually.
  • Ignoring Optional defaults on null fields. In dataclass mode a field whose sample was null gets a None default so you can construct the object without it. If you delete the default but the API sometimes omits the field, deserialization with a plain dict-to-dataclass helper will raise a TypeError. Keep the default.

Best practices for JSON to Python workflows

  • 1.Paste a realistic sample with real nesting and at least one array so every class the converter emits is exercised by your actual data shape.
  • 2.Pick dataclass when you want objects and TypedDict when you want mypy to validate dict keys, and confirm with one round-trip test before committing.
  • 3.Keep the from __future__ import annotations line so forward references to nested classes do not raise at definition time on Python 3.9 and earlier.
  • 4.Regenerate and diff when the upstream API changes, rather than editing annotations by hand, so new fields are not silently dropped.

Frequently asked questions

How does this JSON to Python converter work?

The tool parses your JSON in the browser, infers a Python type for every field (int, float, str, bool, None, lists, and dicts), and emits a class for each nested object. With the dataclass style it adds the @dataclass decorator and field annotations. With TypedDict it emits class Foo(TypedDict) for dict-shaped data. With type hints it emits plain classes with annotations only. Nested objects become their own classes, and repeated array element shapes are merged into one shared class.

What is the difference between dataclass, TypedDict, and type hints?

A dataclass gives you a real class with an auto-generated __init__, __repr__, and __eq__, so you can instantiate objects and access attributes with dot notation. A TypedDict describes a dict shape for static type checkers while the runtime value stays a plain dict, which is ideal when you just want mypy or pyright to validate dict keys. Plain type hints emit bare class definitions without the decorator, useful when you want annotations to paste into an existing module that already imports dataclass.

Does the generated Python run without extra dependencies?

Yes. The output uses only the standard library: dataclasses.dataclass and typing.TypedDict, both of which ship with every modern Python release. The from __future__ import annotations line at the top makes the annotations lazy so forward references to nested classes do not raise at definition time. Copy the output into a .py file and import it directly.

How are nested objects and arrays handled?

Every distinct nested object becomes its own top-level class, listed after the root class. An array of uniform objects produces one shared element class and the field annotation becomes list[ThatClass]. An empty array defaults to list[Any]. A field whose sample value is null is annotated as Optional[...] with a None default so instantiating the dataclass without that field works.

Does this tool send my JSON to a server?

No. Parsing and code generation run entirely in JavaScript inside your browser tab. The page makes no network request with your input and the JSON is discarded when the tab closes. You can disconnect from the internet after the page loads and the converter still works, which makes it safe to paste internal API responses or sample payloads.

Why are the generated field names PascalCase?

The converter mirrors the C# sibling tool so multi-language projects stay consistent: incoming camelCase or snake_case keys become PascalCase fields, and a docstring records the original key so you can map it back. If you prefer snake_case fields for idiomatic Python, rename them by hand after generation or open the sibling JSON to TypeScript tool for a camelCase variant. Keyword collisions such as class or import are escaped with a trailing underscore.

Can I convert JSON to other languages with this tool?

Yes. The same input can be converted to C# classes, TypeScript interfaces, Go structs, and Rust structs using the sibling converters. Paste the same JSON into each one so property names and nesting stay consistent across your full stack.

Related JSON and developer tools