Free tool · Developer tools
JSON to Python dict converter
This JSON to Python dict converter turns pasted JSON into a Python dictionary literal you can drop straight into a script or a test. The sample below, 9 keys over 4 levels, becomes 12 lines of Python with true turned into True and null into None.
- Free
- No sign-up
- Runs in your browser
dict, 7 keys12 lines, 4 levels deep, 231 bytes
- Single quotes
- 4 space indent
- true, false, null become True, False, None
| JSON | Python | Count |
|---|---|---|
| object | dict | 2 |
| array | list | 2 |
| string | str | 5 |
| number without a fraction | int | 3 |
| number with a fraction or exponent | float | 1 |
| true | True | 1 |
| null | None | 1 |
How to use the JSON to Python Dict Converter
- Paste the JSON into Your JSON, or choose a .json file from your device.
- Pick String quotes and a Layout: four spaces, two spaces, or everything on one line.
- Turn on Trailing commas or Escape non-ASCII characters if your project style needs them, and type a Variable name to get an assignment.
- Press Copy or Download .py. An error message gives the line and column of the first problem.
How do I convert JSON to a Python dictionary?
JSON and Python look alike, but they are not the same text. JSON writes true, false and null in lower case, and Python needs True, False and None. The converter reads your JSON, keeps its order, and writes it again as Python source. A JSON object becomes a dict, an array becomes a list, and a string stays a string.
The one-line form of the sample starts {'name': 'Ada Lovelace', 'age': 36, 'active': True, 'spouse': None, 'sco and continues from there. Note that "active": true is now 'active': True, and "spouse": null is now 'spouse': None. The whole literal fits on one line of 177 characters if you choose One line.
This is different from calling json.loads in a program. That call gives you the data while the program runs, and this page gives you code you can paste into a file, a unit test or a REPL, so the data sits next to the rest of the source. Add a variable name and the page writes data = followed by the literal.
What changes when JSON becomes a Python literal?
Besides the three keywords, the main choice is the quote character. Python accepts both, so the page lets you pick: with single quotes the text It's here becomes {'note': 'It\'s here'}, and with double quotes it becomes {"note": "It's here"}. A quote that matches your choice is escaped with a backslash, as are backslashes, line breaks and tabs. Control characters become \x escapes, and a lone surrogate such as '\ud800' stays legal.
Numbers are copied exactly as they were typed. A 29-digit integer stays a Python int with every digit, since Python integers have no fixed size (Python 3.11 and later refuse decimal literals over 4,300 digits by default), and 0.1000000000000000055511151231257827 is copied digit for digit, although Python itself reads it as the nearest float. A number such as 1e999 would overflow a float, so the page warns that Python reads it as inf. An integer of more than 4,300 digits is copied too, with a warning, because Python 3.11 and later refuse to read an integer literal that long. Nesting is limited to 190 levels, since Python stops reading source at about 200 nested brackets. Repeated keys in one object are kept in the output, and a warning reminds you that Python keeps the last value.
Accented letters and other non-ASCII text stay as they are by default, since Python 3 source is read as UTF-8. Turn on the escape option to get ASCII only: São Paulo becomes 'S\xe3o Paulo'. The table lists what was found in the sample.
| JSON | Python | Count |
|---|---|---|
| object | dict | 2 |
| array | list | 2 |
| string | str | 5 |
| number without a fraction | int | 3 |
| number with a fraction or exponent | float | 1 |
| true | True | 1 |
| null | None | 1 |
Why does my JSON not convert?
The page reads strict JSON, the same grammar a JSON parser enforces, so it points at the common slips with a line and a column. A trailing comma, as in {"a":1,}, gives "Not valid JSON. Line 1, column 8: A trailing comma before } is not allowed in JSON.". Single quotes, which are valid in Python and not in JSON, give "Not valid JSON. Line 1, column 2: Keys must use double quotes in JSON, found a single quote.".
Three more cases come up often. Comments are not part of JSON, so a line starting with // is refused. NaN and Infinity are refused for the same reason. Text that already has True, False or None is Python rather than JSON, and the message says so. For a table instead of code, the JSON to CSV converter flattens an array of objects.
A one-line layout is good for short values in a test, and the indented layout suits a fixture file. Lists of plain values that fit in 60 characters stay on one line in the indented layouts, so a list of scores reads as [9.5, 8, 10] rather than three separate lines.
Use it with
- Python Dict to JSON Converter: template-based generators with live preview and copy.
- JSON to CSV: convert JSON arrays to CSV.
- JSON Formatter: format and validate JSON.
Questions
How do I convert JSON to a Python dictionary?
Paste the JSON and copy the result. true, false and null become True, False and None, objects become dicts and arrays become lists. The input {"on":true} becomes {'on': True}.
Why is true not valid in Python?
Python spells its boolean values True and False and its empty value None, with a capital first letter. The converter makes the change for you.
Should I use single or double quotes?
Both are valid Python, so follow your project style. A single-quoted string that holds an apostrophe gets a backslash before it, as in {'note': 'It\'s here'}.
Are big numbers kept exactly?
Yes, as text. Number text is copied as typed, so a 29-digit integer keeps every digit, while a long decimal still becomes the nearest float when Python reads it. A number too large for a float, such as 1e999, gets a warning, and so does an integer of more than 4,300 digits.
What happens with duplicate keys?
They are written out as they appear, with a warning. When Python runs the literal, the last value for that key wins.
Does my JSON leave my browser?
The conversion runs with the code on this page, and no request is sent with your text, so a file with private data can be pasted here.
The tool's results are computed in your browser by the code on this page; the explanations are checked against the tool's own output.