JSON to Python Dataclasses with Type Hints
Paste or open JSON and get Python dataclasses at once; nothing is uploaded.
Processed on your device. Nothing is uploadedHow to check
Paste text instead
Pasted text stays in this tab and is never sent anywhere.
Give your Python code real types for the JSON it reads. Open a config file, an API response or a record from a queue, and the page writes a plain @dataclass for every object in it, in modern Python 3.10 syntax: int where the sample has whole numbers, float where it has decimals, list[str] or list[Project] for arrays, and str | None = None where a value can be missing or null. Field names are snake_case, so postalCode becomes postal_code. Copy the classes into your module or download a .py file. Everything is generated in your browser, so the data is never uploaded.
Why use JSON to Python
Private
Your JSON is read and the dataclasses are written by your own browser; nothing is sent to a server.
Typed for mypy and your editor
Each field carries a type hint, so autocompletion and type checkers know what the data holds.
Works offline
After you have used it once, it keeps working without an internet connection.
How it works: generate Python dataclasses from JSON
Open or paste JSON
Drop or choose a .json file, paste the text into the box, or try a sample.
Read the dataclasses
The Python code is shown at once: one plain dataclass for each kind of object, with typed, snake_case fields.
Copy or download
Copy the code or download it as a .py file, then map the JSON keys to the field names where you load the data.
What the JSON to Python generator handles
- Objects: one plain @dataclass each, with nested objects as their own classes referenced by name.
- Numbers: int when every value seen is whole, float as soon as one has a decimal part.
- Arrays: list[...] typed by their items, such as list[str] or list[Project]; objects in one list are merged into one dataclass.
- Missing or null values: a type such as str | None with a default of None.
- Names: camelCase or dashed keys become snake_case fields, such as postal_code from postalCode.
- Input from a .json file, a drop, the paste box or a sample, up to 10 MB.
JSON to Python: what it can't do
- The code needs Python 3.10 or later, because it uses list[str] and str | None.
- There are no from_dict or to_dict helpers: field names are not mapped back to the JSON keys, and nested dicts are not turned into objects for you.
- Types come from one sample: a field that is a whole number there but a decimal elsewhere will be typed int.
- Very large documents may take a few seconds; files up to 10 MB.
JSON to Python dataclasses: questions and answers
- Is my file uploaded?
- No. The JSON is read and the Python code is generated by your browser, on your device. Pasted text stays in this tab and is never sent anywhere.
- Why does a field read str | None = None?
- Because it was null somewhere in the sample, or missing from some of the objects in an array. str | None means the field holds a string or None, and the default of None lets you leave it out.
- Can I pass my JSON straight to the dataclass?
- Only if the keys are already snake_case. Address(**{"postalCode": "12345"}) fails because the field is postal_code. Rename the keys first, for example with a small dict comprehension, and build nested objects the same way.
- Why did a price become int instead of float?
- Every price in your sample was a whole number, such as 10 or 25. Add one value with a decimal part, such as 10.5, and the field becomes float, or change the hint by hand.
- Which Python version do I need?
- Python 3.10 or later. The hints use the built-in list[...] and the X | None form, which older versions do not accept at runtime.
- Why dataclasses and not Pydantic models?
- Dataclasses are in the standard library, so the code runs with no extra package. If your project uses Pydantic, the fields carry over to a BaseModel, where an alias generator can map camelCase keys for you.
- How do I convert JSON to a Python dataclass?
- Paste JSON into OpenAnyDoc's JSON to Python generator and the dataclasses appear at once, one per kind of object, with typed snake_case fields. Copy them or download a .py file; the JSON is turned into code in your browser without being uploaded.
Why it's private: your files never leave your browser
Most online viewers upload your file to a server, so a copy of it leaves your device. OpenAnyDoc works the other way round: your browser reads the file and draws it on your screen. Nothing is uploaded, there is no account, and the file is gone from memory when you close the tab, so there is nothing for us to store, see or lose.
How we tell you about new features
Announcements are based on the release date shown to everyone, and nothing is stored on your device or sent anywhere.