JSON to Python Dataclass Generator
Paste a JSON sample and get typed Python dataclasses instantly. Nested objects become their own classes defined above the ones that use them, lists infer their element type, and null fields become Optional with a None default. Rename the root class and copy the result.
How to convert JSON to a Python dataclass
- Paste a JSON object into the input box.
- Optionally rename the root dataclass (defaults to Root).
- Copy the generated classes or download them as a .py file.
Examples
Flat object
{"name":"Ada","age":36,"active":true}@dataclass
class Root:
name: str
age: int
active: boolNested object and list
{"user":{"id":1},"tags":["a","b"]}@dataclass
class User:
id: int
@dataclass
class Root:
user: User
tags: List[str]Frequently asked questions
What does this tool do?
It reads a JSON sample and generates matching Python dataclasses, inferring the type of every field and adding the dataclasses and typing imports so you can paste the result straight into your code.
How are nested objects and lists handled?
Each nested object becomes its own @dataclass, named in PascalCase from its key and defined before the class that references it. Lists infer their element type as List[T], a list of objects gets a generated class, and lists with mixed values become List[Union[...]].
How are JSON types mapped to Python?
Strings become str, whole numbers become int, decimals become float, and booleans become bool. A null value becomes Optional[Any] with a None default. JSON has no separate integer type, so a value like 36 maps to int while 1.5 maps to float.
Why are some fields reordered or renamed?
Keys are converted to snake_case, so firstName becomes first_name. Fields that default to None are listed after the required fields because Python forbids a non-default field after a default one, which keeps the generated dataclass valid.
Is my JSON sent to a server?
No. Parsing and code generation run entirely in your browser, so your data never leaves your machine.
Why did I get an error instead of a dataclass?
The tool needs valid JSON with an object at the top level. Invalid syntax or a top-level array, string or number returns an error message instead of classes.
Related tools
JSON to TypeScript
Generate TypeScript interfaces from a JSON sample. Infers nested objects, arrays and union types, then copy or download the .ts file. Runs in your browser.
JSON Formatter
Format, validate and minify JSON online. Pretty-print with custom indentation, sort keys and catch syntax errors. Runs in your browser.
JSON Schema Generator
Generate a JSON Schema from a JSON sample. Infers types, properties and required fields as draft-07, ready to copy or download. Runs in your browser.
.env to JSON
Convert a .env file to JSON, or JSON back to .env. Parses KEY=value lines, comments, quotes and export. Runs entirely in your browser.
Aspect Ratio Calculator
Calculate aspect ratios fast. Enter a ratio like 16:9 and one dimension to get the other, or enter width and height to simplify the ratio.
Base58 Encoder
Encode and decode Base58 online with the Bitcoin alphabet. Convert text to Base58 or back, UTF-8 safe, no confusing 0 O I l. Runs in your browser.