JSON1

JSON1 / JSON to Python

JSON to Python

Generate Python dataclasses with type hints from a JSON sample. Optional fields default to None and are ordered after the required ones, so the class compiles as written.

Processed locally
InputJSON
Ready
OutputPython

Converted output appears here as you type.

Good to know

  • Every record in an array is inspected, not just the first, so a field that appears in only some of them is typed as optional rather than required.
  • Identical nested shapes are emitted once and reused, and reserved words are renamed to something legal in this language.

About this tool

A dataclass gives you attribute access and a readable repr for a JSON payload without writing the constructor. This generates one per object in the sample, with type hints throughout.

Keys are converted to snake_case, and where that changes the name a comment records the original JSON key so the mapping stays visible.

Why are the optional fields at the bottom?
Python raises at class-creation time if a field with a default precedes one without. Optional fields default to None, so they have to come last — the generated class is ordered to compile as written.
Why a dataclass and not a Pydantic model?
A dataclass is in the standard library, so the output runs anywhere Python 3.7 does with nothing to install. Pydantic would add validation, but it would also add a dependency to a snippet you may only want for type hints. If you need the validation, the field list transfers over unchanged.
How are optional fields decided?
Every record in an array is inspected, not just the first. A field that appears in some records and not others is typed as optional, and a value that is sometimes null widens accordingly. Generators that read only the first element get this wrong on exactly the payloads where it matters.
What happens to a key that is a reserved word?
It is renamed to something legal in the target language, and where the language supports it, an annotation records the original JSON key so serialisation still round-trips.