JSON to Python
Paste JSON and get Python classes inferred from its shape, as dataclasses, typed dictionaries, or plain dictionary access.
Classes are generated live as you type. Nothing leaves your browser.
Output style
Options
Python
How to convert JSON to Python online
Paste your JSON
Drop a JSON object, array, or value into the input. The matching Python code appears live as you type.
Name the root class
Set the root class name to whatever suits your code, such as User or ApiResponse.
Pick an output style
Choose dataclasses, typed dictionaries, or plain dictionary access, and toggle snake_case field names and default values to match your project.
Copy or download the result
Copy the generated Python to your clipboard or download it as a .py file.
Why use this tool
Three output styles
Generate @dataclass definitions, TypedDict definitions, or a plain commented list of dictionary key paths, depending on how your project accesses the data.
Nested classes for nested objects
Every nested object becomes its own class instead of an inline shape, and repeated shapes are deduplicated into a single class.
Lists, unions, and optional fields
Array elements are merged into a single list type, mixed elements become a Union, and a field missing from some samples or seen as null becomes Optional.
snake_case with alias notes
Field names can be converted to snake_case to match Python convention, with a comment noting the original JSON key whenever it changed.
Optional defaults toggle
For dataclasses, choose whether Optional fields get an explicit None default, with fields reordered automatically so the generated code stays valid.
Runs entirely in your browser
Everything happens on your device. Nothing is uploaded, so private payloads and API responses stay local.
About this tool
This tool turns a JSON sample into Python code that matches its shape. Paste an object, an array, or a bare value, and it walks the structure to infer types: strings, integers, floats, and booleans map to their Python equivalents, nested objects become their own class, and arrays become a list of the merged element type, with a Union when the elements differ. A field missing from some samples in a list, or seen as an explicit null, is marked Optional rather than silently dropped or guessed at.
Three output styles cover the common ways Python code reads structured data. Dataclasses generate a @dataclass per object, with an option to give Optional fields a None default so the class can be constructed with only the fields you have. Typed dictionaries generate a TypedDict per object instead, for code that keeps working with plain dictionaries but wants type checking on the keys. Plain dictionary access skips classes entirely and prints a commented list of every key path and its inferred type, useful as a quick reference while writing dict-based code by hand.
Field names can be converted to snake_case to match Python convention, and whenever that changes a name, a comment notes the original JSON key so the mapping back to the source is never lost. Everything runs in your browser, so a response body carrying internal service names or tokens never leaves your machine. For the equivalent output in other languages, see JSON to TypeScript and JSON to Go.
Frequently asked questions
- What is the difference between the three output styles?
- Dataclasses generate a @dataclass per object, which behaves like a real typed object with attribute access. Typed dictionaries generate a TypedDict per object, which is still a plain dictionary at runtime but gets type-checked keys. Plain dictionary access skips classes and prints a commented list of key paths and their types, for code that just indexes into nested dictionaries directly.
- How are optional and nullable fields handled?
- A field that is absent from some samples in a list, or that is explicitly set to null in any sample, is wrapped in Optional. For dataclasses, an option adds an explicit None default to those fields and reorders them after the required fields, since Python requires defaulted fields to come last.
- How are mixed-type arrays handled?
- Every element of an array is merged into one type. If the elements differ, for example a mix of strings and numbers, the result is a Union of the types seen. Arrays of objects merge into a single class whose fields cover every object seen.
- What does the snake_case option do?
- It converts field names like userName to user_name to match Python naming convention. Whenever a converted name differs from the original JSON key, a trailing comment notes the original key so nothing about the source shape is lost.
- What happens if my JSON is invalid?
- The tool shows an inline parser error instead of generating anything, and clears it the moment the input parses again. Empty input shows a neutral placeholder rather than an error.
- Is my JSON uploaded anywhere?
- No. Everything runs in your browser. Your JSON never leaves your device, is never sent to a server, and is not stored or logged.
Related tools
JSON to TypeScript
Paste JSON and get typed TypeScript interfaces, with nested objects, arrays, optional fields, and null handled for you.
JSON to Go Struct
Paste JSON and get typed Go structs, with nested types, slices, json tags, pointers, and omitempty handled for you.
CUID Generator
Generate collision-resistant CUIDs in bulk, in the standard or a shorter slug form.
cURL to Fetch Converter
Paste a curl command and get the equivalent JavaScript, with the method, headers, body, and basic auth carried across.
Data URI Generator
Turn any file, image, or SVG into a ready-to-paste data URI, with CSS and img snippets.
Dice Roller
Roll d4 to d100 dice with any count and a modifier, and see each die, the subtotal, and the total.