JSON to Python Dataclasses and TypedDict
Convert JSON into Python dataclasses or TypedDict classes with typing hints, Optional fields and nested classes defined in dependency order. Free, in-browser.
Classes are ordered so dependencies come first. Optional fields default to None; TypedDict output uses NotRequired (Python 3.11+).
Runs entirely in your browser. Your data is never uploaded.
Related Tools
View AllWhat is JSON to Python?
Generate typed Python classes from a JSON sample: either @dataclass classes or TypedDict definitions. Nested objects become their own classes, which are emitted before the classes that use them so the module imports cleanly.
How to use JSON to Python
- Paste a JSON sample into JSON input (or click Load sample / Open file).
- Set the Root type name (default
Root). - Choose Output style:
@dataclassorTypedDict. - Copy the generated Python code or Download it as a file.
Example
{"id":1,"name":"A","tags":["x"]} from dataclasses import dataclass
from typing import List
@dataclass
class Root:
id: int
name: str
tags: List[str] When to use JSON to Python
Use it to add type hints for JSON payloads in scripts, FastAPI code or tests. Dataclasses do not convert nested dicts automatically – use a library such as dacite or pydantic for that, or the TypedDict style to keep plain dicts.
How JSON to Python works
The sample is walked recursively and every position collects the types it has seen. Arrays of objects are merged into one type: a key missing from some elements becomes optional, a key that is null somewhere becomes nullable, integers mixed with decimals become a floating-point type, and anything else that mixes types falls back to a dynamic type. Nested objects become their own named types (PascalCase of the key, array keys singularized: orders → Order), with numeric suffixes when names collide. Python output uses int, float, str, bool, List[T], Optional[T] for nulls and Any for dynamic values. In dataclasses, required fields come first and optional ones get = None; keys that are not valid identifiers (or are keywords like class) are renamed in snake_case with a # JSON key comment. TypedDict output marks missing keys with NotRequired and switches to the functional TypedDict("Name", {...}) syntax when a key is not a valid identifier.
Common Use Cases
- Type-annotate a JSON API response for mypy or Pyright
- Create dataclasses for a config file
- Describe webhook payloads with TypedDict
- Generate starting models before switching to pydantic
FAQs
Which Python version is needed?
Dataclass output works on Python 3.7+. TypedDict output uses NotRequired, available in the typing module from Python 3.11 (or typing_extensions for older versions).
Why are some fields renamed?
Keys such as first-name or class are not valid attribute names, so they become first_name / class_ with a comment showing the original JSON key.
Can I load JSON straight into the dataclass?
Root(**data) works for flat data; nested objects stay dicts unless you convert them yourself or use a helper library.