JSON to Python
Generate Python dataclasses, Pydantic models or TypedDicts from JSON
Paste JSON or load an example to generate Python code.
What JSON to Python generates
JSON to Python turns an API response or any JSON sample into Python type definitions. Dataclass mode writes @dataclass classes plus a from_dict class method, because Root(**data) does not build nested dataclasses or map renamed keys. Pydantic v2 mode writes BaseModel classes and adds Field(alias="original-key") and populate_by_name only where a name changes. TypedDict mode keeps the exact JSON keys and switches to the functional TypedDict("Name", {...}) syntax when a key is not a valid identifier.
How to use
- 1.Paste JSON, upload or drop a .json file, or load one of the examples.
- 2.Set the root type name. If the JSON root is an array of objects, this names the item type and RootList is added as an alias.
- 3.Choose Dataclass, Pydantic v2 or TypedDict and decide whether attributes use snake_case.
- 4.Read the notes under the output, then copy the code or download models.py.
How types are inferred
- •Every array item is inspected, not just the first one, and objects in the same array are merged.
- •A key missing from some objects is optional; a key that is sometimes null is nullable. The two are kept apart.
- •1 and 2.5 in the same position widen to a decimal number; other mixes are reported.
- •Empty arrays and values that are only ever null have no type evidence, so a generic type is used and a note explains why.
- •Type mapping: string → str, integer → int, decimal → float, boolean → bool, array → list[T], object → nested class, unknown → Any, null observed → T | None.
- •Reserved words become class_, keys starting with a digit get a field_ prefix, and colliding names get _2.
Limits and privacy
A sample shows what the data looked like once, not the full API contract. Review optional fields, generic types and number widths before relying on the code. Input is limited to 5 MB and 256 nesting levels; from 2 MB, generation waits for the Generate button. Everything runs in your browser and the JSON is never uploaded. Generated code uses Python 3.10+ union syntax; TypedDict mode with NotRequired needs Python 3.11 or typing_extensions.
