Menu
PDF Tools
Image Tools
Converters & Utilities
Seller & Print Tools
Accounting & Business
Calculators
Text & Writing Tools
Developer & Web Tools
Creator & Social Tools
Browser Utilities & Games
Document & Design Makers
Text & Developer Tools
Exam & Form Tools

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.

How to use JSON to Python Dataclass online

  1. Paste JSON. An object or a list of objects.
  2. Pick dataclass or Pydantic. And set the date option.
  3. Name the top class. Nested names follow it.
  4. 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?
Only if you choose the Pydantic style. Dataclasses are in the standard library.
Why are the fields reordered in a dataclass?
Required fields must come before fields that have a default.
What does the future import do?
It lets a class use another class that is written later in the file. Without it, the page writes classes in dependency order.
Can it handle a top-level list?
Yes. It writes an alias for the list type and a class for the items.
Is Optional the same as a default of None?
Not quite. Optional says the value may be None; the default says it may be left out.
Where does my text go when I use the JSON to Python Dataclass?
It stays in your browser tab; the JSON to Python Dataclass sends nothing anywhere.
Can I copy the JSON to Python Dataclass result?
Yes. Use the copy button on the JSON to Python Dataclass result, or select it and copy it by hand.

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.

More tools in Developer & Web Tools →

Search tools