JSON to Python Converter

Python dataclasses with type hints, generated from a JSON sample

Input

Output

Paste your JSON on the left and the Python code shows up here.

Or drop a file on this box, , or .

What does the JSON to Python converter generate?

json.loads() gives you dicts, and after a while order["customer"]["email"] everywhere gets hard to follow. From one sample, this page writes a class for each object, decorated with @dataclass, with type hints on every field and fields renamed to snake_case the way PEP 8 suggests: subscriberId becomes subscriber_id.

Renaming fields would break RootObject(**data), so every class also gets a from_dict() classmethod that reads the original keys and builds the nested objects for you: account=Account.from_dict(data["account"]), devices=[Device.from_dict(x0) for x0 in data["devices"]]. Keys that are missing from some objects in an array are read with data.get(), and nullable nested objects are only converted when they aren't None. The file starts with from __future__ import annotations, so class order and forward references don't matter.

Three switches change the output. Dataclass off gives a plain class with an explicit __init__. typing Module picks the hint style: Optional[str] and List[str] when on, str | None and list[str] when off. Nullable Types controls whether fields that were null or missing get the optional type. Keys that are Python keywords get a trailing underscore (class_), and keys starting with a digit get an n_ prefix. We checked the generated code by running from_dict() on real payloads in CPython. Here, though, no Python executes: the classes are produced by the page and the JSON is not uploaded.

How to use it

The classes update as you type.

1

Paste a JSON sample

Paste into Input, choose a .json or .txt with Upload, or load Sample. Here is a greenhouse sensor reading; note the lastFault that happens to be null:

{"deviceId":"GH-03","tempC":18.4,"humidityPct":72,"batteryLow":false,"lastFault":null}

Class Name sets the root class. Call it Reading and the fields come out as device_id: str, temp_c: float, humidity_pct: int and last_fault: Any, since a value that was only ever null gives no type to go on. A parse error in the output panel? Run the text through the JSON Validator.

2

Read the classes

The Output panel shows the imports, then the classes with the nested ones first. Flip the three switches to compare the styles.

3

Copy or download

Copy the code or Download a .py file, then root = RootObject.from_dict(json.loads(text)).

Where this saves time

Wrapping API responses

Turn the JSON from requests.get(...).json() into typed objects so your editor can autocomplete order.customer.email and mypy can check it.

Config files

Load a JSON config into a dataclass once at startup and pass a typed object around instead of a dict whose keys nobody remembers.

Data pipelines

Records coming off a queue or out of an S3 export get a fixed shape early, which makes the later transformation steps easier to test.

Seeing which fields are optional

Paste a handful of real records as an array. Anything missing or null in one of them comes out Optional, read with data.get().

Common Questions

Does it generate Pydantic models or TypedDicts?

No, it generates dataclasses (or plain classes) with a from_dict() loader. If you use Pydantic, the field names and type hints are a good starting point: change the base class to BaseModel and add Field(alias=...) for renamed keys.

Which Python version do I need?

Python 3.7 or later. The from __future__ import annotations line keeps hints as strings, so even str | None is fine before 3.10. The output is tested on Python 3.

Does from_dict() validate types?

No. It maps keys and builds nested objects, but it doesn't check that a field marked int really got an int. Add your own checks, or use a validation library, if the input isn't trusted.

How are big integers handled?

Python ints have no size limit, so IDs like 8901240544102066246 are simply int, and json.loads() reads them exactly.

What happens when I paste a JSON array of readings?

The objects in the array become the root class, and the output opens with a comment holding the comprehension that loads them: # The JSON root is an array: [Reading.from_dict(x) for x in data]. Paste ["a", "b"] instead and the output panel explains that a list of strings has no keys to turn into fields.

Is my JSON uploaded?

No. The dataclasses are produced in your browser and the sensor data never travels to a server. The limit is 256 KB; past it you get a message asking you to trim the sample, which costs nothing because a handful of objects describes the shape as well as ten thousand.

Other JSON Tools You Might Need

Same data arriving as XML? XML to Python handles that. Sharing it with a TypeScript front end or a Go service? Those pages build matching types from the same sample, and the formatter tidies a minified response before you start.