Zero Signup ToolsFree browser tools

Developer Tools

JSON to Python Converter

Convert JSON to a Python dict literal, dataclass models, or Pydantic BaseModel classes in your browser. Smart inference for nested objects and lists.

0 chars

Output options

Live preview updates with every change. Inference runs in your browser; the JSON you paste is not uploaded.

Used as the variable name in dict mode and the root class name in dataclass and Pydantic modes. Sanitized to a valid identifier.

String quotes

Black and Ruff default to double quotes. Single quotes match the older PEP 8 example style.

Indent

Controls the indent inside dict literals only; class bodies always use four spaces, matching standard Python tooling.

Optional style

Used in dataclass and Pydantic modes when a field is missing in some samples or can be JSON null. PEP 604 syntax requires Python 3.10+.

Which mode should I use?

  • Dict literal: when you have JSON in hand and just want it as a Python value. The result is a single assignment to a dict, list, or scalar, with True / False / None where JSON had true / false / null. No imports, no schema, no validation; useful for fixtures, REPL snippets, and test data.
  • Dataclass: when you want a type-safe model without a runtime dependency. The tool emits one @dataclass per object shape, with list[X] and Optional[X] (or X | None) inferred from the data. Required fields land before optional fields so the generated class actually constructs cleanly.
  • Pydantic BaseModel: when the data crosses a trust boundary and needs runtime validation (FastAPI request bodies, config loaders, API clients). Non-identifier JSON keys are aliased with Field(alias="...") so the original key still parses, and optional fields default to None so instantiation matches partial payloads.

Notes on inference

  • Numbers without a fractional part map to int, otherwise float. A list whose values include both produces float so the wider type is kept.
  • Object shapes are merged across every array element, so a list with partially overlapping object fields produces a single class that marks the missing fields optional.
  • Empty arrays become list[Any] (or list[Any | None] if the parent is optional). Mixed-type arrays collapse to list[Any].
  • JSON keys that are not valid Python identifiers are converted to snake_case for the field name. In Pydantic mode the original key is preserved with Field(alias="original-key").
  • Python hard keywords (class, def, return, and so on) get an underscore suffix so the generated code parses.
  • Everything runs in your browser. The JSON you paste, the class names you pick, and the generated Python all stay on this device.

How to use

  1. Paste a JSON value into the input on the left, or click Load sample JSON to see a worked example with nested objects, arrays, and a null field.
  2. Switch between Dict literal, Dataclass, and Pydantic BaseModel using the tabs above the output. The live preview updates as you type.
  3. Rename the root: the value becomes the variable name in Dict mode and the root class name in Dataclass and Pydantic modes. Names are sanitized to valid Python identifiers automatically.
  4. Pick the output style: double or single quotes, 2 or 4 space indent, Optional[X] or X | None for nullable fields, and alphabetic or JSON-order key sorting.
  5. Click Copy dict, Copy dataclass, or Copy Pydantic to take the active output to your clipboard, ready to paste into a .py file, a FastAPI route, or a fixture module.

About this tool

JSON to Python Converter turns any JSON value into three different Python outputs from a single paste, all generated locally in your browser. The Dict literal mode renders the parsed JSON as a native Python expression (dict, list, str, int, float, True, False, None) and assigns it to a variable so the snippet drops straight into a .py file, REPL session, fixture, or notebook cell without calling json.loads. The Dataclass mode walks the parsed tree, infers a structural schema (merging object shapes across every array element and tracking which keys are missing or null in some samples), and emits one @dataclass per object shape with modern Python type hints: list[X] for arrays, Optional[X] or X | None for nullable and missing fields, and float when any number in a list is non-integer so the wider numeric type survives. Required fields are emitted before optional ones, matching the constraint Python enforces on default arguments. The Pydantic BaseModel mode emits the same shapes as pydantic.BaseModel v2 classes, the dominant validation library for FastAPI, AWS Lambda handlers, and config loaders. When a JSON key cannot be a Python identifier (hyphens, dots, spaces, a leading digit, a reserved word), the field is renamed to snake_case and the original key is preserved with Field(alias="original-key") so model_validate(json_payload) still works against the wire format. Output options cover the things that get bikeshedded in pull requests: double-quote (Black, Ruff) vs single-quote string literals, 2 vs 4 space indent inside dict literals, the legacy Optional[X] vs the PEP 604 X | None style, alphabetical vs JSON-order key sorting, whether to prepend the dataclass and typing (or pydantic and typing) imports so the snippet is runnable, and whether Pydantic optional fields default to None or stay required with Field(...) ellipses. Useful for modeling third-party API payloads in a FastAPI service, scaffolding strict types from a sample document, generating test fixtures, converting a recorded JSON request into a typed model, prototyping Pydantic schemas, replacing hand-written dataclasses on a config loader, and pasting copied JSON into a Python REPL without manual escaping. Parsing, inference, and code generation run entirely on your device, so the JSON payloads, API responses, and class names you put in here never leave your browser.

Free to use. Works in your browser. No signup, no login.

Related tools

You may also like

All tools
All toolsDeveloper Tools