JSON to Python
Paste a JSON sample. Get Python types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.
Open the tool with Python chosen
An example
This JSON:
{
"id": 42,
"name": "Ada Lovelace",
"active": true,
"joined": "2020-01-02T03:04:05Z",
"tags": ["engineer", "writer"],
"address": { "city": "London", "postcode": null },
"scores": [9.5, 8, 10]
}
becomes this Python:
from dataclasses import dataclass
from datetime import datetime
@dataclass
class Address:
city: str
postcode: None = None
@dataclass
class Root:
id: int
name: str
active: bool
joined: datetime
tags: list[str]
address: Address
scores: list[float]
With the reader and the writer
The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.
from dataclasses import dataclass
from typing import Any, TypeVar, Callable, Type, cast
from datetime import datetime
import dateutil.parser
T = TypeVar("T")
def from_str(x: Any) -> str:
assert isinstance(x, str)
return x
def from_none(x: Any) -> Any:
assert x is None
return x
def from_int(x: Any) -> int:
assert isinstance(x, int) and not isinstance(x, bool)
return x
def from_bool(x: Any) -> bool:
assert isinstance(x, bool)
return x
def from_datetime(x: Any) -> datetime:
return dateutil.parser.parse(x)
def from_list(f: Callable[[Any], T], x: Any) -> list[T]:
assert isinstance(x, list)
return [f(y) for y in x]
def from_float(x: Any) -> float:
assert isinstance(x, (float, int)) and not isinstance(x, bool)
return float(x)
def to_class(c: Type[T], x: Any) -> dict:
assert isinstance(x, c)
and 68 more lines. Open the tool to see the whole file.
What you need in your project
Nothing for a dataclass. `pip install pydantic` for the Pydantic form.
The plain form uses `@dataclass` and writes the reader by hand. It needs no package, and it checks each field as it reads.
Turn on "Pydantic BaseModel" for the form most teams use now. Pydantic checks the data, converts what it can, and gives a clear message for what it cannot.
The Python version option matters: 3.10 and later write `str | None`, and the older versions write `Optional[str]`.
The trap to know about
A key that is not a legal Python name is renamed, so `user-id` becomes `user_id`. The reader keeps the original name, but your own code must use the new one.
The options you can change
These belong to Python, so the row of switches changes with the language.
| Option | What it does | Default |
|---|---|---|
| Python version | Python version | 3.5 | 3.6 | 3.7 | 3.9 | 3.10 |
| Types only | Classes only | false |
| Tidy the names | Transform property names to be Pythonic | true |
| Pydantic BaseModel | Uses pydantic BaseModel | false |
Questions
How do I turn JSON into Python?
Paste your JSON into the box, choose Python, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.
Does my JSON go to a server?
No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.
What do I need to add to my Python project?
Nothing for a dataclass. `pip install pydantic` for the Pydantic form.
How does it know which fields are optional?
From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.
Other languages
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to Code