Skip to content

JSON to Python Class Converter

Paste JSON, get Python classes instantly — dataclass, Pydantic v2, or TypedDict. Correct Optional typing, camelCase aliases, nested classes. 100% in your browser, free.

No Tracking Runs in Browser Free
Output
0 chars
Python Output
0 lines
Output verified against Python 3.12+ syntax and Pydantic v2 semantics for real-world API payloads. — Go Tools Team · Jul 3, 2026

What is JSON to Python conversion?

JSON to Python conversion turns a JSON sample into ready-to-use Python classes — a standard-library dataclass, a Pydantic v2 model, or a TypedDict — so you never hand-write field definitions for an API response or config file. This Python class generator infers correct types (int, float, str, bool, Optional), turns nested objects into named classes, and adds Pydantic aliases for camelCase keys, all 100% in your browser.

Examples

API response → dataclass

{"id":101,"name":"Ada Lovelace","email":"ada@example.com","active":true,"roles":["admin","user"]}
from dataclasses import dataclass
from typing import List


@dataclass
class Root:
    id: int
    name: str
    email: str
    active: bool
    roles: List[str]

A typical REST payload becomes a ready-to-use @dataclass. Whole numbers infer int, decimals infer float, and arrays become List[str].

Nested object (child class first)

{"repo":"pydantic","owner":{"login":"samuelcolvin","id":100}}
from dataclasses import dataclass


@dataclass
class Owner:
    login: str
    id: int


@dataclass
class Root:
    repo: str
    owner: Owner

Nested objects become separate, named classes. The child class (Owner) is emitted before the parent so the code runs without from __future__ import annotations.

camelCase → Pydantic v2 alias

{"login":"octocat","publicRepos":15,"createdAt":"2011-01-25"}
from pydantic import BaseModel, ConfigDict, Field


class Root(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    login: str
    public_repos: int = Field(alias="publicRepos")
    created_at: str = Field(alias="createdAt")

In Pydantic mode, fields are snake_case with Field(alias=...) mapping back to the original JSON key, and populate_by_name lets you build the model either way — so validation round-trips real API data.

Array of objects → TypedDict (optional key)

{"users":[{"id":1,"nick":"x"},{"id":2}]}
from typing import List, Optional, TypedDict


class User(TypedDict):
    id: int
    nick: Optional[str]


class Root(TypedDict):
    users: List[User]

Arrays of objects merge into one element type. A key missing from some items (nick) becomes Optional. TypedDict describes the shape of a plain dict with zero runtime overhead.

How to convert JSON to Python

  1. 1

    Paste your JSON

    Drop a JSON object, array, or API response into the input box. Conversion starts instantly.

  2. 2

    Pick dataclass, Pydantic, or TypedDict

    Toggle the output style to match your project, and rename the root class from the default Root.

  3. 3

    Copy or download

    Grab the generated Python with one click and paste it straight into your code.

Common Use Cases

Typed API clients
Turn a sample REST or GraphQL response into Pydantic models or dataclasses for a requests or httpx client without writing field definitions by hand.
Config and fixture parsing
Generate models for JSON config files, test fixtures, or webhook payloads you need to load and validate.
Fast prototyping and type hints
Paste an unfamiliar payload to instantly see its shape as Python types — a quick way to explore a new API or add TypedDict hints to legacy dict code.

How the conversion works

Structural inference
Each object becomes a named class; identical shapes are deduplicated so you get one class, not copies. Arrays of objects are merged key by key, and keys absent from some items become Optional.
Three idiomatic targets
dataclass keeps zero dependencies and preserves valid keys for Root(**data); Pydantic v2 adds Field aliases plus populate_by_name for real validation; TypedDict describes a plain dict for static type checkers, using functional syntax when a key is not an identifier.
Correct, dependency-light typing
Whole numbers map to int (arbitrary precision, no overflow), decimals and exponents to float, and null-only fields to Optional[Any]. typing imports (Optional, List, Any) are added only when actually used.
100% client-side
Parsing and generation run in your browser with no network calls, so your data stays private.

Tips for clean Python models

Match the mode to the job
Use dataclass for internal data holders, Pydantic v2 when you validate or parse untrusted input, and TypedDict when you only need static type hints over existing dicts.
Paste a representative sample
Fields are marked Optional only when a sample omits them, and null-only values fall back to Any. A filled-in, complete example yields the most precise types.
Name your root class
Set a meaningful root name (User, ApiResponse) instead of the default Root for readable, importable code.

Frequently asked questions

How do I convert JSON to a Python class?
Paste your JSON into the input box. The converter parses it instantly in your browser and generates Python on the right. Pick dataclass, Pydantic v2, or TypedDict with the toggle, then click Copy — no upload, no account, no waiting.
What is the difference between dataclass, Pydantic, and TypedDict output?
dataclass gives you a standard-library @dataclass with no dependencies — great for plain data holders. Pydantic v2 emits BaseModel classes that validate and coerce data at runtime, ideal for parsing untrusted API responses. TypedDict describes the shape of a plain dict for static type checkers (mypy, Pyright) with zero runtime cost. Switch modes to compare the same JSON in each.
How do I generate a Pydantic model from JSON?
Choose the Pydantic v2 tab. Fields are converted to snake_case with Field(alias="originalKey") so the model still reads camelCase JSON, and model_config = ConfigDict(populate_by_name=True) lets you construct it by field name too. Parse a payload with Root.model_validate(data) — Pydantic validates types and raises a clear error on bad input.
How does the dataclass output handle camelCase keys?
A dataclass has no built-in alias, so to keep Root(**data) working, dataclass mode keeps the original key as the field name when it is a valid Python identifier (publicRepos stays publicRepos). If you want idiomatic snake_case with aliases, use the Pydantic v2 mode instead, which maps snake_case fields back to the exact JSON key.
How are optional and null fields typed?
When a key appears in some array items but not others, its type is wrapped in Optional. A field that is only ever null becomes Optional[Any], because JSON null alone carries no type. Paste a representative sample with a filled-in value to get a more specific type than Any.
What Python type does each JSON value map to?
Strings map to str, booleans to bool, whole numbers to int, and any number with a decimal point or exponent to float. Because Python integers are arbitrary precision, even huge IDs stay int — there is no 64-bit overflow. Empty or mixed-type arrays become List[Any], and objects become nested classes.
Does it handle nested objects and arrays of objects?
Yes. Each nested object becomes its own named class, and identical shapes are deduplicated into a single class reused by every field. Arrays of objects are merged key by key so you get one element class, with keys missing from some items marked Optional. Child classes are always emitted before the classes that use them.
How are Python keywords and non-identifier keys handled?
A JSON key that is a Python keyword (class, from, import) gets a trailing underscore (class_). Keys with hyphens, spaces, or a leading digit are sanitized to valid identifiers in dataclass and Pydantic modes. In TypedDict mode, any dict containing a non-identifier key is emitted with the functional TypedDict('Name', {...}) syntax so the exact key like "first-name" is preserved.
How do I use the generated dataclass to parse JSON?
For a flat object, json.loads then Root(**data) works directly. For nested structures, dataclass does not recurse automatically — either build the child objects yourself, use a library like dacite or pydantic.dataclasses, or switch this tool to Pydantic v2 mode, where Root.model_validate(json.loads(text)) parses the whole tree in one call.
Is my JSON data private and safe?
Yes. Conversion runs 100% in your browser with JavaScript. Your JSON — including tokens, IDs, or customer data — never leaves the page and is never sent to a server.
Is the tool free? Do I need an account?
It is completely free with no sign-up, no limits, and no ads cluttering the workspace. It works offline once the page has loaded.

Related Tools

View all tools →