Python

Python Dict to JSON Converter

Turns a Python literal into valid JSON and names every type that had to change on the way.

Loading the tool…

Processing happens locally in your browser. What you paste or load is processed by this page and is not uploaded to a server. Nothing is stored unless you use a control that says it stores something, and you can clear anything this site has kept from the privacy page.

How to use this tool

  1. Paste the dict, list, tuple or set. Comments and trailing commas are fine.
  2. Set the indent, or use 0 to get the JSON on one line.
  3. Select Convert to JSON.
  4. Read the summary. If it is marked as a warning, it names every type that had to change to fit into JSON.

What python dict to json does

A Python dict printed to a log or copied out of a shell is very nearly JSON, and the small differences are exactly the ones that break a parser: True instead of true, None instead of null, single quotes around strings, a trailing comma, a tuple where JSON expects an array. This page reads the literal the way Python would and writes proper JSON out the other side. Nothing is executed — the text is parsed, not evaluated, so a literal from an untrusted log cannot do anything.

Python has types JSON simply does not have, and the honest thing is to say so rather than convert silently. A tuple and a set both become arrays, and both are reported. A numeric or boolean dictionary key becomes a string, because JSON allows nothing else, and that moves it to the front of the object. nan and infinity become null. Comments are dropped. Whenever any of that happens the result is marked as a warning and the summary lists what changed, so you can decide whether the JSON still means what the Python meant.

Frequently asked questions

No. Nothing on any of these pages runs Python. The literal is read character by character by a parser written in JavaScript, the same way a JSON parser reads JSON — it recognises dicts, lists, tuples, sets, strings with any prefix, numbers with underscores, True, False and None, and it refuses anything else. That means a dict copied out of a log you do not control cannot do anything here, because there is nothing for it to do.

Both become JSON arrays, because JSON has neither type, and both are reported in the summary so the conversion is not silent. A set also loses its ordering guarantee — it never had one — so the order you see in the output is the order the items were written in, not anything meaningful.

JSON keys must be strings, so an integer or boolean key is converted to its string form and that is reported as a warning. One side effect is worth knowing about: a key that looks like a number moves to the front of the object, because that is how a JavaScript object orders its keys. If key order matters to you, quote the keys in the Python before converting.

This page reads a literal — one dict, list, tuple, set, string or number. It is not a Python parser, so an assignment, a function call, an f-string with an expression in it, or a variable name will be refused with the line number where the trouble starts. Strip it back to the value itself and it will convert.