JSON QuickFix › Convert a Python dict to JSON

Convert a Python dict to JSON

Printing a dict in Python gives {'ok': True, 'value': None}, which looks like JSON but isn't. Paste the output below and press Quick Fix to turn it into valid JSON you can use in an API, a test fixture or a config file.

Input

Paste some JSON, drop a .json file, or load the sample to get started.
Advertisement

Why printed dicts aren't JSON

print(my_dict) and repr() produce Python syntax, not JSON. The differences are small but every one of them breaks a JSON parser:

  • Strings use single quotes: 'name' instead of "name"
  • Booleans are capitalised: True / False instead of true / false
  • None instead of null
  • Trailing commas are allowed

Quick Fix converts all of these at once and lists each change.

Do it in Python instead

If you still have the object in Python, the most reliable route is the standard library: import json; print(json.dumps(my_dict, indent=2)). Use this page when all you have is text, for example from logs, a notebook output, a bug report or a chat message.

Things to watch for

Values with no JSON equivalent, such as tuples, sets, datetime objects or Decimal('1.5'), appear in printed output as Python expressions. Quick Fix will point to them as errors. Convert them in Python (for example with default=str in json.dumps) or edit them by hand.

Advertisement

Frequently asked questions

How do I convert True, False and None to JSON?

JSON uses lowercase true, false and null. Quick Fix replaces Python's True, False and None with those values, but only outside strings, so text like "None of the above" is left alone.

Is the converted JSON identical to json.dumps output?

The data is the same. Formatting may differ slightly, and you can choose 2 spaces, 4 spaces or tabs, or minify it.

Is my data uploaded?

No. The conversion runs entirely in your browser.