Python Dict to JSON

Paste a printed Python dict and get valid JSON with proper quotes, true/false and null.

0 characters
Indent
Keys

Why this conversion needs its own parser

When you print() a Python dictionary, or paste one from a Jupyter notebook, debugger or log line, what you get back is Python's own literal syntax — not JSON. The two look almost identical but differ in exactly the spots that make a strict JSON parser fail: single quotes instead of double, True/False/Noneinstead of true/false/null, and Python-only types like tuples and sets that JSON has no syntax for at all.

This tool runs a small Python-literal parser (not eval(), and nothing is executed) that understands that syntax directly, then re-serialises the result as JSON — so you can take output straight from a Python script and paste it into a config file, an API request body, or a JSON-only tool.

Example

A dict as Python prints it:

{'id': 7, 'active': True, 'ref': None, 'tags': ('a', 'b')}

becomes:

{
  "id": 7,
  "active": true,
  "ref": null,
  "tags": [
    "a",
    "b"
  ]
}

What gets converted

Doing it in Python instead

If you're already inside a Python process, this is the direct equivalent of what the tool does:

import json, ast
data = ast.literal_eval(python_repr_string)
print(json.dumps(data, indent=2))

ast.literal_eval is the safe alternative to eval() — it only accepts literal values, never runs code.

Python Dict to JSON FAQ

Why can’t I just use JSON.parse on a printed Python dict?

Python’s repr() uses single quotes and the literals True, False and None, none of which are valid JSON — JSON.parse rejects them immediately. This tool understands Python’s literal syntax specifically and converts it to the equivalent JSON.

What happens to tuples and sets?

Both become JSON arrays, since JSON has no equivalent of either. Order is preserved for tuples; sets have no defined order in Python either, so their order in the output matches whatever order they were written in.

Does it handle nested dicts, nested nesting, and multi-line dicts pasted from a debugger?

Yes — any level of nesting, trailing commas, and Python comments (#…) are all handled, which covers most values copied from a REPL, a debugger watch panel, or a print() call.

What if a value is a variable name, not a literal?

The converter only understands literal values (strings, numbers, True/False/None, lists, dicts, tuples, sets) — the same set JSON can represent. A bare variable name like some_var has no fixed value to convert, so it is reported as an error rather than guessed at.