JSON to Python Dataclass Converter
This JSON to Python converter turns a sample payload into dataclasses with type hints. Keys are converted to snake_case to match Python conventions, and any key colliding with a keyword gets a trailing underscore. Field order matters here in a way it does not in other languages: a dataclass field with a default cannot precede one without, so optional fields are emitted last with a default of None — get that wrong and the module raises as soon as it is imported. Arrays become List of the inferred item type, and a field that was null throughout becomes Optional[Any].
How it works
Paste a representative JSON sample and the Python declarations appear on the right. The types are inferred from what is actually in the sample, so the more complete your example, the better the result — one array element with an extra field is enough to have that field marked optional rather than missed.
Each object becomes a dataclass with type hints, and keys are converted to snake_case to match Python conventions.
A dataclass field with a default cannot precede one without, so optional fields are emitted last with a default of None — the generated module imports cleanly rather than raising at definition time.
Generation runs entirely in your browser — nothing you paste is uploaded.
How to convert JSON to Python dataclasses
- Paste a representative JSON payload into the input on the left.
- Name the root class if you want something other than
Root. - Copy the generated module into your project.
Naming and ordering
Keys are converted to snake_case to match Python conventions, and any key that collides with a
keyword gets a trailing underscore. Optional fields are emitted last with a default of None, because
a dataclass field with a default cannot precede one without — get that order wrong and the module raises as soon
as it is imported.
Types you get
- Strings, integers, floats and booleans map directly.
- Arrays become
List[...]of the inferred item type. - Optional and nullable fields become
Optional[...]. - A field that was null everywhere becomes
Optional[Any], since the sample reveals no type.
Give it a representative sample
The types are inferred from the data you paste, so the quality of the output depends on how complete your example is. Two things are worth including: an array with more than one element, so fields that appear in some records but not others get marked optional rather than assumed mandatory; and real values rather than placeholders, so numbers are recognised as numbers and whole numbers are told apart from decimals.
What inference cannot tell you
A sample shows what the data was, not what it can be. A field that happened to be null in every record you pasted has no discoverable type, and an enum looks exactly like a string. Treat the generated dataclasses as a first draft that saves the typing, then tighten the parts you know more about than the sample does.
Frequently asked questions
How do I generate Python classes from JSON?
Paste a representative payload into the left panel and copy the generated dataclasses. They import cleanly as a module with no further edits.
Why are optional fields listed last?
A dataclass field with a default cannot come before one without — Python raises a TypeError at class definition time. Optional fields therefore go last with a default of None, so the module imports rather than failing.
Are field names converted?
Yes. Keys become snake_case to match Python conventions, and a key that collides with a keyword gets a trailing underscore so it remains a legal identifier.
What if a field was null in every record?
It becomes Optional[Any], because the sample reveals no type at all. Replace Any with the real type once you know it.
Can it generate Pydantic models instead?
Not directly, but the field names and types are the same — change the decorator to a BaseModel subclass and the annotations carry over.
Is my JSON uploaded?
No. Generation runs entirely in your browser.
Related tools
JSON to TypeScript Converter
This JSON to TypeScript converter turns a sample payload into exported interfaces.
JSON to JSON Schema Generator
This JSON to JSON Schema generator produces a draft 2020-12 schema from a sample payload.
JSON to Java Class Converter
This JSON to Java converter generates POJOs with Jackson annotations, getters and setters.