Data Format Conversions

Python Dict to JSON Converter

Convert Python dicts, lists, tuples and sets to valid JSON: True/False/None, single and raw strings, comments and NAME = value assignments are handled with a safe parser (nothing is executed), and keys follow json.dumps rules.
Runs in your browserLive result

Workspace

Options

[1, 2, 3] instead of one item per line.

How to use this tool

  1. 1Paste a Python dict or list, or NAME = value assignments, or open a .py file.
  2. 2Read any notes about values JSON cannot hold (undefined, NaN, tuples…).
  3. 3Choose indentation and key sorting.
  4. 4Copy or download the .json file.

Frequently asked questions

No. A safe parser (like ast.literal_eval) reads only literal values. Function calls, variables and comprehensions are reported as errors with their line and column.

Tuples and sets become JSON arrays. Integer, float, True/False and None keys become strings ("1", "true", "null"), exactly as json.dumps does. Tuple keys cannot be represented and are reported.

float('nan') and float('inf') are not valid JSON, so they become null with a note. Python's json.dumps would write NaN, which most JSON parsers reject.

Yes. The output of print(my_dict) or repr() is a Python literal, so it converts directly.