JSON to Python Dict

Paste JSON and get a Python literal back, ready to assign to a variable or paste into a REPL.

0 characters

Or drop a .json/.txt file onto the box above.

Indent
Keys

How to use

  1. Paste JSON (an object, array, string, number, boolean or null) exactly as an API response, config file or JSON.stringify printed it.
  2. Choose the indent: 2 spaces, 4 spaces, Tab, or Single line for one unbroken statement.
  3. Tick Sort keys alphabetically if you need a stable order for diffing against another version.
  4. If it does not convert, the error names the character position JSON.parse stopped at. JSON is stricter than Python about quotes, commas and comments.
  5. Copy to clipboard, or Download .py to save it as a snippet.

Why JSON needs translating before it is valid Python

JSON and Python's dict/list syntax look almost interchangeable, which is exactly what makes copying one into the other by hand risky: JSON requires double-quoted strings and spells its constants true, false and null, while Python requires the capitalized True, False and Noneand is happy with either quote style. Paste raw JSON into a .py file and it mostly runs, until it hits one of those three keywords and raises a NameError.

This tool parses the JSON with the same rules a browser's JSON.parse uses, then re-serialises the result as Python source (the reverse of the site's Python Dict to JSON converter). Nothing is eval()'d in either direction.

Example

JSON as an API or config file would show it:

{"id": 7, "active": true, "ref": null, "tags": ["a", "b"]}

becomes:

{
    'id': 7,
    'active': True,
    'ref': None,
    'tags': [
        'a',
        'b'
    ]
}

Options explained

Edge cases and common errors

Do the same thing in code

If you're already inside a Python process, you don't need a text conversion at all, since json.loads() gives you the native object directly, and printing it already uses Python's own quotes and constants:

import json
data = json.loads(json_text)
print(data)

print(data) renders single quotes and True/False/None the same way this tool does, but on one line. For a multi-line, indented layout closer to this page's default, use pprint.pformat(data, indent=1, sort_dicts=False). It will not match this tool's formatting exactly (it wraps long lines rather than always breaking one item per line), but it is Python's nearest built-in equivalent.

JSON to Python Dict FAQ

Why doesn’t Python’s json module just do this?

json.loads() already parses JSON into native Python objects; this tool exists for when you don’t have a Python process running, only a browser: pasting an API response or a config file here gives you back the exact literal source you would type to recreate it, ready to paste into a script.

Why are booleans and null capitalized in the output?

That is Python syntax, not a stylistic choice: true/false/null are JSON-only keywords, while Python spells the same values True/False/None. Pasting the JSON spelling into Python source raises a NameError.

Does key order change from what I pasted?

No. Every key keeps the position it was written in, including digit-only keys such as "2" and "1", which a plain JavaScript object would move to the front in numeric order. That matches what Python’s own json.loads gives you. Tick Sort keys alphabetically if you want a fixed order instead.

Can I use the result as-is in my code?

Yes. The output is a plain Python literal (dict, list, string, number, boolean or None), so data = <paste> or ast.literal_eval() on it both work without modification.

Runs entirely in your browser: this page is a static file with no server behind it, so nothing you paste can be uploaded. Last reviewed by Parthesh Gupta. Implements Python language reference: literals.