Free tool · Developer tools
Python dict to JSON converter
Paste a Python dict or list and get JSON back. A line such as {'admin': True, 'manager': None} becomes {"admin": true, "manager": null}, and the page lists every change it made so you can see what Python wrote differently.
- Free
- No sign-up
- Runs in your browser
- 8 keys
- 14 lines
- 152 bytes
- Depth 3
{
"name": "Ada",
"age": 36,
"admin": true,
"manager": null,
"tags": [
"math",
"code"
],
"scores": {
"1": 9.5,
"2": 8
}
}
| Python | JSON |
|---|---|
| True / False (1) | true / false |
| None (1) | null |
| Tuples (1) | Arrays |
| Single-quoted strings (9) | Double-quoted strings |
| Numbers and text as keys (2) | String keys |
| Trailing commas (1) | Removed |
How to use the Python Dict to JSON Converter
- Paste the dict, list or other literal into Python dict, list or other literal. The box starts with an example.
- Pick a Layout: 2 spaces, 4 spaces, tab, one line or compact with no spaces.
- Tick Sort keys to order keys alphabetically, or Escape non-ASCII to write accented letters and emoji as \u codes, as Python does by default.
- Read the JSON and the table of changes, then press Copy JSON or Download .json.
How do I convert a Python dict to JSON?
In Python the usual way is json.dumps(d). That needs a running interpreter and a variable that already holds the dict, which is not the case when you only have the text, for example a printed dict from a log or a bug report. Pasted text cannot go straight into a JSON parser, because Python and JSON disagree on several details. This page reads the literal itself and writes JSON the way json.dumps would.
The example in the box has 8 keys. Single quotes become double quotes, True becomes true, None becomes null, the tuple ('math', 'code') becomes an array, the trailing comma is dropped, and the keys 1 and 2 of the inner dict become the strings "1" and "2". The same input on one line is 114 characters: {"name": "Ada", "age": 36, "admin": true, "manager": null, "tags": ["math", "code"], "scores": {"1": 9.5, "2": 8}}.
What is different between a Python dict and JSON?
A Python dict literal allows single-quoted strings, the capitalized words True, False and None, tuples, trailing commas, comments, numbers in hex, octal and binary, digit separators such as 1_000, and keys that are not strings. JSON allows none of these. Every one is converted, and the table below the result lists how many of each were found in your text.
Keys need care. JSON keys must be text, so an integer, float, boolean or None key is written as text: True becomes "true", None becomes "null" and 1.5 becomes "1.5". A key that is a tuple has no JSON form and gets an error. If a key appears twice, Python keeps one entry with the last value in the position of the first one, and the converter does the same. Python counts True, 1 and 1.0 as one key, so {True: 1, 1: 2} becomes {"true": 2}. The integer 1 and the string '1' are different keys in Python, so both stay, and the JSON then holds two "1" keys exactly as json.dumps writes it. Many JSON parsers keep only the last of them.
| Python | JSON on one line |
|---|---|
| {True: 'x', None: 'y', 1.5: 'z', 2: 'w'} | {"true": "x", "null": "y", "1.5": "z", "2": "w"} |
| {'k': 1, 'k': 2, 'a': 3} | {"k": 2, "a": 3} |
| {1: 'x', '1': 'y'} | {"1": "x", "1": "y"} |
| {True: 1, 1: 2} | {"true": 2} |
| (1,) | [1] |
| 'it\'s' | "it's" |
Why is my Python dict not valid JSON?
The usual causes are single quotes, True, False and None, and a trailing comma. Pasting a printed dict into a JSON validator fails on the first of these. Floats are another trap. Python writes 1.0 where many converters write 1, and it switches to exponent form at 1e16 and below 0.0001. The page keeps Python's spelling, so the JSON shows exactly what json.dumps would.
The JSON formatter then validates, minifies or reformats the result, and the JSON to CSV converter and JSON to TypeScript converter take it further. Integers of any size stay exact, so 123456789012345678901234567890 is not rounded.
| Python float | JSON number |
|---|---|
| 1.0 | 1.0 |
| 100.0 | 100.0 |
| 1e15 | 1000000000000000.0 |
| 1e16 | 1e+16 |
| 0.0001 | 0.0001 |
| 1e-05 | 1e-05 |
What does this converter read, and what does it refuse?
It reads the literals that Python reads with ast.literal_eval: dicts, lists, tuples, strings in single, double and triple quotes with the r and u prefixes, adjacent strings that Python joins, escapes such as \n, \xe9, \u00e9 and \U0001F600, integers, floats, True, False and None. A number may carry one sign, so -5 and +5 pass and --5 does not, as in Python. Nothing is executed. Sets such as {1, 2} are refused on purpose, because json.dumps raises a TypeError for them; write the items as a list instead. Function calls such as dict(a=1), names, f-strings, bytes and complex numbers are refused with a message that gives the line and column. JSON style true, false and null are accepted and noted in a chip.
Everything runs in the page while you type. The text is not uploaded, and there is a limit of one million characters.
Use it with
- JSON to Python Dict Converter: template-based generators with live preview and copy.
- JSON Formatter: format and validate JSON.
- JSON to CSV: convert JSON arrays to CSV.
Questions
How do I convert a Python dict to JSON?
Use json.dumps(d) in Python, or paste the text of the dict into this page. It converts quotes, True, False, None, tuples and keys the way json.dumps does.
Why does json.loads fail on my Python dict?
A dict printed by Python is not JSON. It uses single quotes and True, False and None, which JSON does not allow. Convert it first, or build it from the object with json.dumps.
What happens to None, True and False?
None becomes null, True becomes true and False becomes false. If they are dict keys they become the strings "null", "true" and "false".
What happens to tuples and sets?
A tuple becomes a JSON array. A set is refused with a message, because json.dumps raises a TypeError for sets. Change the set to a list in square brackets and convert again.
Can I convert a dict with integer keys?
Yes. JSON keys must be strings, so {1: 9.5} becomes {"1": 9.5}. Tuple keys cannot be converted and give an error that points to the key.
Does it use eval?
No. A small parser reads the literal, so nothing in the text is run. Anything that is not a plain literal, such as a function call or a variable name, is refused.
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.