Input

Output

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. Paste a JSON sample and you get one class per 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. The output is tested by running from_dict() on the input. It all runs in your browser.

How to use it

The classes update as you type.

1

Paste a JSON sample

Paste into the Input panel, Upload a .json or .txt file, or click Sample. Example input:

{"subscriberId":"SUB-1001","plan":"Unlimited 5G","dataUsage":45.2,"status":"active"}

Set Class Name for the root class. If the input won't parse, check it with 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 if the JSON root is an array?

The objects inside it become the root class, and a comment shows the list comprehension that loads them. A root that is null, a string or a list of plain values gives a short message instead of code.

Is my JSON uploaded?

No. The classes are generated by JavaScript in your browser and nothing is sent to a server. Input is capped at 256 KB.

Other JSON Tools You Might Need

Need the same data in another language, or want to clean it up first?