Tools · Python

Python dict to JSON

Paste what print() or pprint wrote. Single quotes, True, False, None and the trailing comma become JSON — listed by line first, applied when you say.

no signup · runs entirely in your browser · ⌘V works anywhere on this page
See it done · 9 s
Python dict to JSON — A printed dict: single quotes, True / None, a trailing comma — the same fixes, named by line, then Fix all. · 9 s

What you get

  • Turns the pprint sample into valid JSON in 37 fixes — 37 fixes in 4 kinds: 32 × single → double quotes, 2 × python false → json false, 2 × python true → json true, 1 × python none → json null
  • Single quotes become double quotes, with a double quote inside the string escaped
  • True, False and None become true, false and null
  • Keeps pprint’s layout — only the quotes, literals and commas change
  • Converts the result to YAML, CSV, a Markdown table or a TypeScript interface from the same screen

What it will not do

  • Turn a tuple, a set, a datetime(...) or a Decimal(...) repr into JSON. Those have no JSON spelling; a line that holds one stays a problem on the list.
  • Run Python. Nothing is evaluated — the text is repaired as text, so a repr that would need executing to read is not read.
  • Send your text anywhere. The page is static and the engine runs in this tab — open the network panel and watch it stay empty.

The four rules

Single quotes become double quotes
{'event': 'charge.completed'}{"event": "charge.completed"}
True, False and None become true, false and null
{'ok': True, 'error': None}{"ok": true, "error": null}
A trailing comma goes
{'a': 1,}{"a": 1}
A tuple is not JSON

('a', 1) has no JSON form. The fix is refused rather than guessed — write a list, ["a", 1], and it reads.

An apostrophe inside a string survives
{'name': "O'Brien"}{"name": "O'Brien"}

The quotes are rewritten token by token, never by a global replace — so the apostrophe in the value stays a letter.

What Python says: JSONDecodeError
{'event': 'x'}Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

json.loads stops at the first single quote and reports the column. Every fix above answers this message.

What Node and Chrome say: SyntaxError
{'event': 'x'}Expected property name or '}' in JSON at position 1 (line 1 column 2)

The same text through JSON.parse — measured by the JSON.parse this site is built with. Older V8 said “Unexpected token ' in JSON at position 1”.

The sample

What the first sample opens — a real file, not an illustration written for this page.

stripe-event.pprint.txt
{'id': 'evt_1PZ3xK2eZvKYlo2C',
 'object': 'event',
 'api_version': '2024-06-20',
 'created': 1719491700,
 'livemode': False,
 'pending_webhooks': 1,
 'type': 'payment_intent.succeeded',
 'data': {'object': {'id': 'pi_3PZ3xJ2eZvKYlo2C0t1Z0QcT',
…

in VS Code

The same fixes, on the lightbulb and in the Problems panel: single quotes, True, False and None, each a named fix on its line.

unstringify for VS Code14-day trial · Free and Pro

Questions

Why is a Python dict not JSON?

Python writes strings with single quotes and spells its literals True, False and None; JSON allows only double quotes and true, false, null. Everything else — braces, brackets, colons, commas — is the same, which is why the fix is mechanical.

Does it handle nested dictionaries and lists?

Yes. The sample nests four levels deep and holds a list of objects; every quote and literal at any depth is one fix.

What about a dict printed on one line?

The same. pprint’s line breaks are only whitespace; a one-line repr from print() is fixed the same way.

Is json.dumps not enough?

It is, while you still have the process. This page is for the text after the fact — a log line, a ticket, a pasted terminal — when the object is gone and the repr is what you have.

Try it on the file that broke your afternoon.

free · no signup