JSON to Python Dataclass
Paste JSON and get Python classes for it, as dataclasses or as Pydantic models. Nested objects become classes, optional fields get Optional, and names are changed to snake_case.
- Free, no sign-up
- Nothing is sent to a server
- Worldwide
How to use JSON to Python Dataclass online
- Paste JSON. An object or a list of objects.
- Pick dataclass or Pydantic. And set the date option.
- Name the top class. Nested names follow it.
- Copy the Python code. Paste it into your module.
Why use this free JSON to Python Dataclass
- Dataclass or Pydantic
- snake_case names with aliases
- Python keywords made safe
- Optional and List typing
- Dates as datetime
- Valid field order
Two styles
A dataclass is part of the Python standard library and holds data in typed fields. A Pydantic model also checks the data when it is loaded. Choose the style with one setting. The page writes the imports at the top, so the result is ready to paste into a file.
Names and keywords
JSON keys in camelCase are changed to snake_case, which is the Python habit. If a key is a Python word, such as from or class, an underscore is added so the code still runs. In Pydantic mode the original key is kept as an alias, so loading the original JSON still works.
Nested classes and lists
Every nested object becomes its own class, named from its parent. Lists become List of the item type. When a list of objects has keys that are missing from some items, those fields are optional and get a default of None.
Field order
A dataclass cannot place a field without a default before a field with one. The page puts the required fields first and the optional ones after, so the code is valid. Pydantic does not have this rule, so it keeps the order of the sample.
Limits
The classes describe the sample. A value that is sometimes a number and sometimes text becomes Any. Date and time text can become datetime, but the dataclass does not convert it for you, so add your own parsing. Nothing is uploaded; the JSON is read in your browser.
Using the tool
There is no button to wait for: the JSON to Python Dataclass result follows every change. Press Try a sample to see how it behaves.
JSON to Python Dataclass: frequently asked questions
Does it need Pydantic installed?
Why are the fields reordered in a dataclass?
What does the future import do?
Can it handle a top-level list?
Is Optional the same as a default of None?
Where does my text go when I use the JSON to Python Dataclass?
Can I copy the JSON to Python Dataclass result?
Related searches for JSON to Python Dataclass
- JSON to Python Dataclass online
- free JSON to Python Dataclass
- JSON to Python Dataclass online free
- JSON to Python Dataclass for mobile
- JSON to Python Dataclass without sign up
- convert json to python dataclass
- json to python dataclass converter
- JSON to Python Dataclass – Developer & Web Tools
JSON to Python Dataclass is a free online tool on MAITBOX, in the Developer & Web Tools group. It runs in your browser, so what you type is not sent to a server. It works on Android and iPhone, and on Windows, Mac and Linux, with no download and no account.