JSON to Python Dataclass Converter
Paste a sample JSON object or array to generate matching Python @dataclass classes with type hints. Nested objects become their own named classes.
How to use it
Paste a representative JSON sample and click Generate. Field names that aren't valid Python identifiers (e.g. starting with a digit) are sanitized. Types are inferred from the single sample provided — this generates plain typed classes, not JSON (de)serialization logic.
Type hints are a starting shape
The converter maps JSON objects to plain Python classes with annotated fields, arrays to typed lists, and primitives to familiar Python types. The output helps document a payload and gives static checkers more information, but Python does not automatically validate assignments from type hints. Parsing, validation, construction, and error reporting still need an explicit runtime path.
Wire keys may not be Python identifiers
JSON keys may contain spaces, hyphens, reserved words, or leading digits. A generated safe field name can therefore differ from the original key. Plain classes do not remember that mapping. Add manual conversion or configure the serialization library already used by the project before expecting user-id to populate user_id. Keep aliases stable when the wire contract is public.
Optional, null, and missing are different
A field observed as null becomes broad because one sample does not reveal its intended non-null type. A field present in the sample may still be optional in other responses. Model absence and nullability from the API contract, not guesswork: an optional attribute, a value that accepts None, and a required value with a default have different runtime behavior.
Collections and numeric types
Mixed arrays produce a union of observed element types, while empty arrays provide no element evidence. Simplify overly broad unions when the contract has a discriminator or a known base model. JSON integers and decimals map naturally to Python numbers for many uses, but money, very large values, and precision-sensitive measurements may require Decimal, strings, or custom decoding.
Choose the runtime model intentionally
The generated plain classes are deliberately dependency-free. A real application may prefer dataclasses, typed dictionaries, a validation library, or hand-written domain objects depending on how data is constructed and checked. Convert the output to the project's existing pattern, validate several positive and negative fixtures, and keep network data untrusted until validation succeeds. Use JSON Schema Generator when an interoperable contract is more useful. Conversion remains local to this tab.
Frequently Asked Questions
Does this handle JSON key → field name mapping for parsing?
No — it only generates the type shape (field names and hints). If a key isn't a valid Python identifier, the generated field name won't exactly match the original JSON key; you'd need a library like dataclasses-json or manual mapping to deserialize.
What if an array has mixed element types?
It becomes List[Union[T1, T2, ...]], listing every distinct element type observed.
What about null values?
A null field is typed Any, since a single sample can't tell you what other type it might hold.