Skip to content

Menu

JSON to Python

Convert JSON to Python dataclasses or TypedDicts

Runs entirely in your browserConvertersFree, no sign-up
Loading tool

01What is JSON to Python?

You've got a JSON response and a Python codebase that runs mypy or Pyright. This tool writes the type definitions for you, as standard library @dataclass classes or as TypedDict subclasses. No third-party package is needed for either one. The rules follow the typing module docs. Strings map to str, whole numbers to int, other numbers to float and booleans to bool. A null in the sample becomes Optional[Any], because one null tells us nothing about the real type. Arrays become List[...] of their element type. If ints and floats are mixed, the list widens to List[float]. Truly mixed lists become a Union, such as List[Union[int, str]]. A null among the items wraps the type in Optional, and an empty list is List[Any]. Every nested object turns into its own class, named in PascalCase from its key. A name that would hide a typing import or a keyword, such as List or None, gets a trailing underscore (List_). Classes with exactly the same fields are merged, so a list of orders gives one OrdersItem. The nested classes print first, so the module runs from top to bottom without forward references. The two modes handle keys differently, and that matters. A TypedDict describes a plain dict, so its keys must match the JSON exactly. The tool keeps them as they are, and isActive stays isActive. If any key isn't a valid identifier, like full-name or class, it switches to the functional TypedDict("Root", {...}) form described in PEP 589. Dataclass fields are renamed to valid snake_case instead. isActive becomes is_active, class becomes class_, and 2fa becomes field_2fa. A small example shows the difference. The input {"user_id":7,"full-name":"Ann","meta":null} in Dataclass mode gives user_id: int, full_name: str and meta: Optional[Any]. In TypedDict mode, you get Root = TypedDict("Root", {...}) with the key "full-name" kept intact. The import block lists only the typing names you need. A top-level array becomes an alias such as Root = List[RootItem]. Some limits to keep in mind. Dataclasses carry no alias metadata, so Root(**data) fails on renamed keys, and nested dicts aren't turned into nested dataclasses. The tool sees one sample, so it can't tell an optional key from a required one. All of this runs as JavaScript in your browser tab, and the JSON you paste stays on your machine.

Python developers adding mypy or Pyright checks to code that handles JSON from APIs, files or queues use it to get dataclasses or TypedDicts without typing them out. The tool runs entirely in your browser, so your data never leaves your device.

02How to use JSON to Python

  1. Paste a representative JSON object or array into JSON Input, or load one of the examples
  2. Type the top-level class name into Root name
  3. Pick Dataclass or TypedDict, to get either plain classes or typed dicts
  4. Read the classes and imports in Python Output and fix any Optional[Any] types
  5. Click Copy and paste the code into your module

03Worked examples

Input: {"user_id":7,"full-name":"Ann","address":{"zip_code":"0150"},"meta":null} (Dataclass)
Result: from dataclasses import dataclass from typing import Optional, Any @dataclass class Address: zip_code: str @dataclass class Root: user_id: int full_name: str address: Address meta: Optional[Any]
Address prints before Root. full_name won't be filled from the key full-name automatically, since dataclasses have no aliases.
Input: {"user_id":7,"full-name":"Ann","meta":null} (TypedDict)
Result: from typing import TypedDict, Optional, Any Root = TypedDict("Root", { "user_id": int, "full-name": str, "meta": Optional[Any], })
full-name can't be a class attribute, so the tool uses the functional TypedDict syntax and keeps the exact key.
Input: {"big":9007199254740993,"mixed":[1,"a"],"ints":[1,2.5]} (TypedDict)
Result: from typing import TypedDict, List, Union class Root(TypedDict): big: int mixed: List[Union[int, str]] ints: List[float]
Python ints have no size limit, so big stays int. A mixed list becomes a Union, and 1 with 2.5 widens to float.
Input: {"class":1,"items":[{"id":1},{"id":2,"qty":3}]} (Dataclass)
Result: @dataclass class ItemsItem: id: int @dataclass class ItemsItem1: id: int qty: int @dataclass class Root: class_: int items: List[Union[ItemsItem, ItemsItem1]]
The keyword class becomes class_. Items with different keys become separate classes in a Union. Imports trimmed.
Input: {"isActive":true,"vals":[1,null]} (TypedDict)
Result: from typing import TypedDict, List, Optional class Root(TypedDict): isActive: bool vals: List[Optional[int]]
TypedDict keeps isActive as written. The null item makes the element Optional[int].
Input: [{"id":1},{"id":2}] (Dataclass)
Result: from dataclasses import dataclass from typing import List @dataclass class RootItem: id: int Root = List[RootItem]
A top-level array becomes a type alias, and identical items share one class.

04Common use cases

  • Typing a third-party REST response in a Flask service so Pyright flags a misspelled key before it ships
  • Describing the records in a JSON Lines file read by a data pipeline, using TypedDict so the rows stay plain dicts
  • Scaffolding dataclasses for an internal tool's config file, then filling in defaults by hand
  • Adding hints to legacy code that passes json.loads() results around as untyped dicts
  • Documenting the shape of an LLM's JSON output in a codebase that avoids adding Pydantic as a dependency
  • Checking quickly how many distinct object shapes an API returns, by seeing whether the tool produces one item class or a union

05Tips

  • TypedDict mode is the safer choice for camelCase JSON. The keys match exactly, so json.loads() output type-checks with no conversion step.
  • Dataclass mode renames keys without aliases. Build a small from_dict function, or use a library like dacite, to map isActive onto is_active.
  • If you see ItemsItem and ItemsItem1 in a Union, the array items had different keys. Merge them and mark the extra field NotRequired (TypedDict) or give it a default (dataclass).
  • A null-only field comes out as Optional[Any]. Swap in the real type once you've seen a non-null value, or mypy won't catch much there.
  • On Python 3.9 and newer you can replace List[...] with the built-in list[...] if your style guide prefers it.

06Related concepts

dataclasses, TypedDict, type hints, mypy, snake_case.

Frequently Asked Questions

What is JSON to Python?

JSON to Python is a free, browser-based developer tool on AIDToolStack. Convert JSON to Python dataclasses or TypedDicts It requires no installation or sign-up and works entirely in your browser.

Is JSON to Python free to use?

Yes. No sign-up, no fees and no usage cap.

Does JSON to Python store my data?

No. It runs in your browser; nothing is uploaded.

Cheat sheet

Reviews and commentsShow

Reviews

Sign in to leave a review.

Loading reviews…

Comments

Sign in to leave a comment.

Loading comments…

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe