JSON to Python
Paste JSON and instantly get typed Python @dataclass definitions, generated entirely in your browser.
Example
Input JSON:
{ "id": 1, "user": { "name": "Ada", "tags": ["x", "y"] } }Generated Python:
from dataclasses import dataclass, field
from typing import Any, List, Optional
@dataclass
class Root:
id: int
user: User
@dataclass
class User:
name: str
tags: List[str] = field(default_factory=list)
How it works
It parses your JSON, walks the structure to infer field types (int, float, bool, str, lists, nested objects), and emits @dataclass classes with a Root type plus PascalCase classes for every nested object. Conversion runs live as you type.
Good to know
JSON to Python turns a pasted JSON payload into ready-to-use Python @dataclass definitions with inferred field types, generating everything live in your browser as you type. It's aimed at Python developers who consume APIs, read config files, or work with database documents and want typed, autocomplete-friendly models instead of passing raw dicts around their codebase.
Reach for it when you have a representative JSON sample and need a quick scaffold: prototyping an API client, adding type hints to legacy code, or seeding a request/response model before wiring up validation. The top-level object always becomes a class named Root, and every nested object becomes its own PascalCase class named after the key that held it, so a single paste can produce several linked classes at once.
To read the output, scan the generated classes from Root downward. Required scalars appear first; fields that are lists get = field(default_factory=list) and nullable fields get = None, which is also why those are pushed to the bottom of each class (Python forbids non-default fields after default ones). A trailing inline comment like # userName next to a field means the original JSON key was renamed to a valid snake_case Python identifier, so you may need a serialization alias to map it back.
One important caveat: types are inferred from your sample, not from a schema. For arrays, only the elements present are examined and lists collapse to a single type (mixed int/float becomes float, anything genuinely mixed or empty falls back to Any), so feed it a sample that includes every field and a fully populated array element to avoid surprises like an unexpected Optional[Any] or a too-narrow list type.
Frequently asked questions
How are nested objects and arrays handled?
Each nested object becomes its own @dataclass with a PascalCase name derived from its key, and arrays become List[T] where T is inferred from the first elements. Mixed numeric arrays collapse int+float to float, and other mixed arrays fall back to Any.
What happens with null values or missing types?
A null field is typed as Optional[Any] and given a None default so the dataclass stays valid. Empty arrays become List[Any], and fields that cannot be inferred default to Any.
Is my data uploaded anywhere?
No — it runs entirely in your browser. Your input never leaves your device and it works offline once loaded.
Is it free?
Yes, completely free with no sign-up and no limits.
People also ask
Does JSON to Python generate Pydantic models or plain dataclasses?
This tool emits standard library @dataclass definitions using dataclasses and typing only, not Pydantic. Pydantic adds runtime validation and parsing; dataclasses are lighter and have no third-party dependency, but they do not validate types at runtime.
Why are some fields placed at the bottom of the generated class?
Fields with defaults (lists set to field(default_factory=list) and nullable fields set to None) must come after fields without defaults, because Python raises an error if a non-default argument follows a default one. The tool reorders fields automatically to keep the dataclass valid.
How do I convert the generated dataclass back from a dict?
You can construct an instance with MyClass(**data) for a flat object, but nested dataclasses are not built automatically. For deep conversion you typically write a from_dict method or use a library such as dacite or dataclasses-json.
What does field(default_factory=list) mean in the output?
It gives a list field a fresh empty list as its default each time an instance is created. A plain mutable default like = [] would be shared across all instances, which is a common Python bug, so default_factory avoids it.
Why did a key in my JSON get renamed in the Python output?
JSON keys can contain characters or reserved words that are not valid Python attribute names, so they are converted to snake_case identifiers. The original key is preserved in an inline comment so you can map it back when serializing.
Can dataclasses enforce the types shown in the generated code?
No. Type hints on dataclasses are not checked at runtime by Python itself; they are used by static type checkers like mypy or Pyright and by editors for autocomplete. For runtime enforcement you would need a validation library.
What happens if my JSON array contains objects with different fields?
The tool infers the array element type primarily from the elements it sees and creates a single class for object elements. If structures differ significantly, fields seen in only some elements may be missed, so it is best to pass a sample whose array items share a consistent shape.
Is the converted code safe to use with sensitive JSON data?
The conversion runs entirely in your browser and the input is never sent to a server, and it works offline once loaded. The generated Python itself contains whatever literal values you would later assign, so review output before sharing if your sample includes secrets.
Related tools