JSON to Python Dataclasses
Example
Input:
{"name": "Ali", "age": 30}Output:
from dataclasses import dataclass
from typing import List, Optional
@dataclass
class Root:
name: str
age: intAbout this converter
The converter maps JSON to the dataclass shapes Python developers actually write: str/int/float/bool hints (integers distinguished from floats by the sample value), nested objects extracted into their own @dataclass, arrays as List[ItemType] with item classes generated from array elements, and fields missing from some array items given a None default — placed last, as dataclass field ordering requires defaults at the end, which is exactly why the merger tracks optionality. For production use with real APIs, consider pairing the output with pydantic or attrs validation; the generated dataclasses are the faithful structural draft.
Frequently Asked Questions
Why do optional fields show "= None" instead of Optional[...]?
The default-None pattern is the compact dataclass idiom; wrap the hint in Optional[...] if your type checker demands it.
What if a field is null in the sample?
It types as None in the draft — replace it with the real type your API guarantees; a null sample cannot reveal it.