JSON to Python Class
Generate Python dataclasses from JSON
About JSON to Python Class
JSON to Python Class generates Python 3.7+ dataclasses that describe the shape of a JSON sample: type definitions, not a dict holding your values. It runs quicktype-core in your browser. Each nested object becomes a @dataclass, field names are converted to snake_case (first-name becomes first_name), and types use int, float, str, bool, List, and Optional from typing, plus Union when a value has two types and Any for an empty array. Fields absent from some array elements get Optional[...] = None and are placed after the required ones, as dataclasses require. There is no from_dict or to_dict helper: you create the objects yourself.
Example
Two people; only Ada has a spouse, so spouse becomes Optional.
JSON input
[{"first-name": "Ada", "age": 36, "spouse": {"name": "Bob"}},
{"first-name": "Lin", "age": 29}]Python output
from dataclasses import dataclass
from typing import Optional
@dataclass
class Spouse:
name: str
@dataclass
class RootElement:
first_name: str
age: int
spouse: Optional[Spouse] = NoneHow to convert JSON to Python dataclasses
- 1Paste your JSON into "JSON input"; an array of several records gives better types than a single one.
- 2Click "Convert" to see the dataclasses in "Python output".
- 3Check renamed fields: keys that clash with Python keywords get a prefix, such as root_class for class.
- 4Click "Copy", or "Download" for a "model.py" file.
Formats and limits
- Input
- JSON object, or array of objects; an array of plain values such as [1, 2] gives an empty output
- Output
- Python dataclasses with typing annotations, saved as model.py
- Names
- snake_case fields; a top-level array gives a class named RootElement
- Python version
- 3.7 or later (dataclasses module)
Where your JSON is processed
The dataclasses are produced locally, in your browser, by the quicktype engine that loads on your first conversion; your JSON is not transmitted to us. If the tool displays an error, the message text is reported to our error log with the tool's name and your browser and its version, after quoted parts, long numbers, and addresses are stripped from it.
Frequently asked questions
Does it create a dict with my data?
No. It writes dataclass definitions that describe the data. To get a dict with your values, use json.loads on the text; to fill a dataclass, pass the matching values to its constructor.
How are optional and null fields typed?
Optional[T] = None, when a key is missing from some array elements or null in some of them. A key that is null in every element has nothing to infer from and is typed None, so give it a real value in the sample if you want a type.
Does it generate Pydantic models?
No. The output uses the standard dataclasses module only. Because the annotations are ordinary typing hints, you can turn a class into a Pydantic model by replacing @dataclass with a BaseModel subclass.
Are whole numbers and decimals kept apart?
Yes. Whole numbers are typed int, and numbers written with a decimal point or an exponent are typed float, even 10.0, so a later value such as 10.5 still matches the annotation.
Tips and tricks
- ✓Rename RootElement or Root to a name that describes your records before you use the classes.