What JSON to Python does
JSON to Python turns a sample JSON document into Python code: one @dataclass per object, with a type for every field inferred from the value in the sample.
JSON to Python examples
The built-in sample
Input
{"name":"Ada Lovelace","active":true,"tags":["math","code"],"address":{"city":"London"}}Output
from dataclasses import dataclass
from typing import Any, List
@dataclass
class Address:
city: str
@dataclass
class Root:
name: str
active: bool
tags: List[str]
address: AddressThis is the input that the Load sample button fills in.
Awkward keys, a null, a decimal and arrays
Input
{"order-id":7,"total":19.5,"paid":false,"coupon":null,"items":[{"sku":"A1","qty":2},{"sku":"B2","qty":1,"gift":true}],"tags":[]}Output
from dataclasses import dataclass
from typing import Any, List
@dataclass
class Items:
sku: str
qty: int
gift: bool
@dataclass
class Root:
order_id: int
total: float
paid: bool
coupon: Any
items: List[Items]
tags: List[Any]The sample has a key with a hyphen (order-id), a null (coupon), a decimal (total), an array of objects whose second item has an extra field, and an empty array.
Each output was produced by running this tool on the input above it.
How it works
The sample is walked recursively and each value is mapped to a Python type:
| Sample value | Python type |
|---|---|
| String | str |
| Whole number | int |
| Decimal number | float |
| true / false | bool |
| null, or an empty array’s items | Any |
| Array of T | List[T] |
| Object | a new named type |
Using the generated code
A dataclass does not convert nested dictionaries by itself. The line below fills the top level only; nested objects stay plain dicts unless you convert them, or use a library such as dacite or pydantic.
root = Root(**json.loads(text))Details and limits
- Nested classes are written before the classes that use them, so the module can be imported as it is.
- A key that is a Python keyword, such as
class, is emitted unchanged and is a syntax error until you rename it. - All fields are required positional fields. Give optional ones a default, and keep fields without defaults first.
- A type is named after the key that holds the object:
itemsgives a type calledItems, with no attempt to make it singular. The top-level type is alwaysRoot. - If two different objects sit under the same key name in different places, two types with the same name are generated. Rename one of them.
- Objects in an array are merged into one type. A field that only some of them have is still generated as a required field.
- Input that is not valid JSON stops the tool, and the status line shows where the problem is. The JSON Validator page lists the usual causes.
Questions
Is my data uploaded when I use JSON to Python?
No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.
What does JSON to Python do?
JSON to Python turns a sample JSON document into Python code: one @dataclass per object, with a type for every field inferred from the value in the sample.
Are there any limits I should know about?
Nested classes are written before the classes that use them, so the module can be imported as it is. A key that is a Python keyword, such as class, is emitted unchanged and is a syntax error until you rename it.
Is there a size limit?
There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.
Does it work on phones, and is it free?
Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.