JSON to Python Converter
Convert a JSON object into Python dataclasses instantly in your browser, with types inferred automatically.
How to use this tool
Writing dataclass definitions by hand to match an API response or config file is tedious and easy to get subtly wrong — especially remembering that Python requires every required field to come before any field with a default value. This tool reads a JSON sample and generates matching Python @dataclass definitions automatically — inferring str, int, float, bool, List, and Optional types, and splitting nested objects into their own named classes rather than one giant nested structure.
For example, {"name": "Alice", "address": {"city": "NYC"}} generates:
from dataclasses import dataclass
@dataclass
class Address:
city: str
@dataclass
class Root:
name: str
address: AddressPaste your JSON into the input box on the left, set the Class name you want for the top-level type, and click Convert. The generated Python appears in the output box on the right, with any parse errors shown below.
FAQ
Is this JSON to Python converter free to use?
Yes, this tool is completely free to use with no limits on how often you can use it.
Does my JSON get uploaded anywhere?
No. The dataclasses are generated directly in your browser. Your data never leaves your computer.
How does it handle arrays of objects with slightly different fields?
It merges the shapes of every object in the array into a single dataclass. A field present in only some elements — or that's null in some of them — becomes Optional with a default of None, rather than being dropped or causing an error. Required fields are always listed before optional ones, since Python requires that ordering in a dataclass.
Why do some number fields come out as float instead of int?
If every sample of a field is a whole number, it's typed int. If any sample has a decimal point, it's typed float — and if a field mixes both across samples, the whole field is typed float rather than a Union, since Python's own typing rules already treat int as valid wherever float is expected.
What happens to JSON keys that aren't valid Python identifiers?
Keys with characters Python doesn't allow in a name (like hyphens or a leading digit) are converted to a safe equivalent — "foo-bar" becomes foo_bar, for example. This means constructing an instance directly from the original JSON dict (MyClass(**data)) won't work for those specific fields unless you rename the keys first to match; every other field, which is the common case, round-trips normally.
Related Tools
JSON to TypeScript Converter
Generate TypeScript interfaces from a JSON sample, with types inferred automatically.
Regex Tester
Test regular expressions with live-highlighted matches and groups.
Cron Expression Parser
Explain a cron expression in plain English and see its next 5 run times.
cURL to Python/JavaScript Converter
Convert a cURL command into equivalent Python (requests) or JavaScript (fetch) code.