JSON to Python
Paste JSON and get Python types for it below, as you type: dataclasses, Pydantic models or TypedDicts, or the data itself as a dict literal.
Three styles
- Dataclass: the standard library. Each class has a from_dict that reads the JSON's keys, nested objects and lists included: Root.from_dict(json.loads(text)).
- Pydantic: BaseModel classes, with Field(alias=…) where a key isn't written in snake_case; Root.model_validate(data) checks the types as it reads.
- TypedDict: types for the dictionaries json.loads gives, keys exactly as in the JSON, and NotRequired for the ones some items lack (Python 3.11).
Written for Python 3.10 and later
Types are written list[str], dict[str, int] and str | None, and each class comes before the ones that use it, so the file runs as it is. Fields are in snake_case; one that would be a keyword or self ends in an underscore (class_).
Types from sample data
Every item of a list, and every value of JSON Lines, is a sample of the same type: a key that some lack is optional, a value that is sometimes null is nullable, whole numbers mixed with decimals are decimals, and objects with the same keys share one type, named after the key they are under ("line_items" holds LineItem). An object whose keys are ids or dates, such as {"1001": {…}, "1002": {…}}, is a map. YAML, TOML, CSV and the other formats work too: pick their language above the editor.
Or the data as a dict
Data writes the JSON as a Python literal, with True, False and None, to paste into a test or a script.
Copy, download or switch
The Python follows your typing, and a mistake in the JSON is pointed out first, with its line and column. Copy or Download (a .py file) take it as it is. Above the code, switch between Types and Data, or pick another language; the Convert menu's Code item opens the one you picked last.
Private: nothing is uploaded
Formatting, checking and comparing all happen in your browser. Your documents are saved in the browser's own storage on this computer, not on a server, so they never leave your device.
With your permission, we count anonymous visits (which pages and tools are used), never what you type. See Privacy.
Questions
Which style should I pick?
Pydantic if you already use it (FastAPI does) or want the data checked; dataclasses for no dependencies; TypedDict to keep working with plain dictionaries, typed.
How do I load the JSON?
data = json.loads(text), then Root.from_dict(data) for dataclasses or Root.model_validate(data) for Pydantic. A TypedDict needs nothing more: data: Root = json.loads(text).
Is my data uploaded anywhere?
Your documents aren't: everything runs in your browser, and they are saved on your computer, in the browser's storage for this site. With your permission we count anonymous visits, never their content.
More tools
- Formatter & Converter for JSON, YAML, XML, SQL
- JSON to TypeScript – Interfaces from JSON
- JSON to Go Struct Converter
- JSON to C# Class Converter
- JSON to Java Class & Record Converter
- JSON to Kotlin Data Class Converter
- JSON to Dart Model with fromJson & toJson
- JSON to Swift Codable Struct Converter
- JSON to Rust Struct Converter (serde)
- Free Online Diagram Maker & Editor
- Crypto Tools: Passwords, Keys, Hashes & JWTs
- Timestamp, Cron, Regex & UUID Utilities