JSONGuy
← All posts

How to convert a Python dict to JSON (the code way and the no-code way)

2026-08-31

Converting a Python dict to JSON is easy in code — and sometimes you just want to paste it somewhere and get the JSON out without writing a script. Here are both ways, plus the edge cases that trip people up.

The code way: json.dumps

In Python, the standard library does it in one line:

import json

d = {
    "name": "John",
    "age": 30,
    "active": True,
    "nickname": None,
}

print(json.dumps(d, indent=2))

Output:

{
  "name": "John",
  "age": 30,
  "active": true,
  "nickname": null
}

Notice how True and None become true and null. That's Python doing the conversion for you.

The no-code way: paste it

When you've printed a dict to the console and just want the JSON, a converter is faster than spinning up a script. The catch: most JSON tools reject a Python dict outright, because it isn't valid JSON. So you paste and get an error, and you're back to manual cleanup.

A converter that understands Python syntax skips that whole step. Paste this and it comes out as clean JSON:

{
  'name': 'John',
  'active': True,
  'nickname': None,
  'tags': ('a', 'b', 'c'),
}

Edge cases worth knowing

  • Tuples become lists. (1, 2) has no JSON equivalent, so it maps to [1, 2].
  • Non-string keys. {1: "a"} becomes {"1": "a"} — JSON keys are always strings.
  • Sets and dates aren't JSON. json.dumps throws on a set or datetime, so you'd need a custom encoder — or just convert those fields first.
  • Single quotes in your paste. Python prints strings with single quotes; strict JSON needs double quotes. A Python-aware converter handles it automatically.

Which one to use

Writing a script? Use json.dumps. It's correct and it's already there. But if you're just eyeballing some output or debugging a response, paste it into the Python dict to JSON converter and get valid JSON instantly — no script, no manual quote-fixing, and nothing leaves your browser.