How to use
- Paste JSON (an object, array, string, number, boolean or
null) exactly as an API response, config file orJSON.stringifyprinted it. - Choose the indent: 2 spaces, 4 spaces, Tab, or Single line for one unbroken statement.
- Tick Sort keys alphabetically if you need a stable order for diffing against another version.
- 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.
- 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
- Indent: 4 spaces / 2 spaces / Tab / Single line sets the output layout. 4 spaces is the PEP 8 convention this tool defaults to; Single line writes the whole literal on one row (still valid Python, e.g.
{'a': 1, 'b': [1, 2]}, just not pretty-printed; there is no equivalent of JSON minification for source code). - Sort keys alphabetically sorts every object's keys at every depth in code-point order, matching Python's own
json.dumps(obj, sort_keys=True); uppercase keys sort before lowercase ones. - Load sample inserts a small JSON object with a nested array and a
null; Clear empties the box.
Edge cases and common errors
- JSON is stricter than it looks. A trailing comma (
{"a": 1,}) stops with "Expected double-quoted property name"; Python's own dict literals allow trailing commas, JSON does not. Single-quoted JSON, like{'a': 1}, stops with "Expected property name or '}'", and unquoted keys, such as{a: 1}, with "Expected property name or '}'". None of these are valid JSON even though all three are valid Python. - Comments are not allowed. Unlike Python's
#comments (handled by the site's Python Dict to JSON tool), JSON has no comment syntax at all: a//or/* */block anywhere in the input fails the same way a syntax error would. - Duplicate keys keep the last value, exactly as Python's own dict literal would if you typed it directly:
{"a": 1, "a": 2}becomes{'a': 2}. - Digit-only keys stay where they were written.
{"2": "b", "1": "a", "x": "c"}becomes{'2': 'b', '1': 'a', 'x': 'c'}. A JavaScript object would list"1"and"2"first in numeric order; this tool reads the keys straight from your text instead, so the order matches what Python'sjson.loadsgives. - Large integers keep every digit.
{"id": 12345678901234567890}becomes{'id': 12345678901234567890}. Numbers are copied from your text as written rather than through a JavaScript number, which would round anything above 253 (this one to12345678901234567000). Python's integers have no size limit, so the literal reads back exactly.1.0stays1.0and1E5stays1E5, both valid Python floats. - Control characters are escaped, so the output always parses: a carriage return becomes
\rand other invisible characters\x00-style escapes, where a raw one would end the string literal or, for a NUL byte, be rejected as source code. - Size: a 4 MB JSON document takes well under a second to parse and re-render on a laptop, and typing is debounced so the page stays responsive while you edit.
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.