Tools · Python

# Python dict to JSON

The text print() writes is valid Python and invalid JSON by exactly four rules. Each one is a fix here, listed by line, applied when you say.

JSON, YAML, XML, CSV, a log file, or something broken. It stays in this tab.

no signup · runs entirely in your browser · ⌘V works anywhere on this page

## What it does

- 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
- Rewrites only the changed spans — pprint’s hanging indent stays, and JSON does not mind it
- 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 sample

The text below is what the first chip opens — a real document the engine reads, not an illustration written for this page.

```
{'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',
…
```

## How

- Copy the dictionary out of your terminal, log or ticket and paste it above.
- The Head says how many changes make it JSON; Show me lists them by line.
- Press Fix all, then Convert, Copy or download the result.

## 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.

## Behind this page

## Try it on the file that broke your afternoon.

free while in alpha · no signup

---

Canonical: https://unstringify.com/tools/python-dict-to-json
