Developer Tools

JSON to Python Dictionary

Paste JSON and get it as Python code. The dictionary literal mode writes the data with Python’s True, False and None, properly escaped strings and your choice of quotes and indentation. The class modes infer types from the sample and generate dataclasses, TypedDict definitions or Pydantic models, with snake_case field names and aliases for keys that are not valid Python names.

  • Runs in your browser
  • No sign-up
  • Free to use

How to use JSON to Python Dictionary

  1. Paste a JSON sample.
  2. Choose a dictionary literal or a class style.
  3. Set the variable or root class name and the formatting.
  4. Copy the code or download data.py.

JSON to Python Dictionary features

Dictionary literal

True, False, None and Python string escaping.

Dataclasses

Typed fields with defaults for optional values, in the right order.

TypedDict

NotRequired for keys missing from some objects.

Pydantic models

BaseModel with Field(alias=…) for renamed keys and datetime detection.

Modern typing

list[str] and str | None with from __future__ import annotations.

Local

Runs in your browser.

When to use JSON to Python Dictionary

  • Pasting API data into a Python test or notebook.
  • Creating Pydantic models for a FastAPI endpoint from an example payload.
  • Typing JSON configuration with TypedDict for mypy.
  • Converting fixtures from a JavaScript project to Python.

JSON to Python Dictionary FAQ

Why not paste JSON directly into Python?

JSON uses true, false and null, which are not Python names. Pasting JSON fails with NameError unless you convert those values or call json.loads().

Which class style should I choose?

Pydantic validates and converts data at run time and is the usual choice for APIs. Dataclasses are part of the standard library but do not convert nested dictionaries themselves. TypedDict only adds type hints to plain dicts.

What happens to keys like "user-id" or "class"?

They become valid names such as user_id and class_. Pydantic models keep the original key with an alias; dataclasses note it in a comment.

Which Python version is needed?

The class output uses list[str] and str | None annotations, which work from Python 3.7 with the __future__ import. NotRequired needs Python 3.11 or typing_extensions.

Are dates converted?

In Pydantic mode, ISO date strings are typed as datetime, which Pydantic parses automatically. Other modes keep them as str.

Is my JSON uploaded?

No. The conversion runs in your browser.

JSON data in Python

Python’s json module turns JSON into dictionaries, lists, strings, numbers, booleans and None, so JSON data maps naturally onto Python’s built-in types. The notation differs, though: JSON’s true, false and null are True, False and None in Python, and Python string literals have their own escaping rules. The dictionary literal mode writes data in Python notation so it can live in source code, tests or notebooks.

Larger projects usually want classes rather than raw dictionaries, so that editors can complete field names and type checkers can catch mistakes. The class modes infer a type for every field from the sample: nested objects become classes named after their keys, arrays become list types, and fields that are missing from some objects or null in some places become optional.

Each class style suits a different need. Dataclasses are part of the standard library and give typed attributes with almost no ceremony, but they do not turn nested dictionaries into nested dataclasses on their own. TypedDict keeps data as plain dictionaries and only informs type checkers such as mypy and Pyright. Pydantic models validate and convert incoming data at run time, which is why frameworks such as FastAPI use them.

JSON keys often follow JavaScript conventions such as camelCase or contain hyphens, while Python code uses snake_case. The generator converts field names and keeps the link to the original key: Pydantic fields get an alias, so model_validate reads the JSON as it is, and dataclass fields carry a comment with the JSON key. Reserved words such as class get a trailing underscore.

As with any inference, the result reflects the sample. Include several array items and representative optional fields for the best types, and review the generated classes against the real data contract before relying on them.

Other useful tools