Python Dict to JSON Converter
Paste your broken, error-filled JSON or Python Dictionaries here. We intelligently fix Single Quotes, Trailing Commas, Missing Quotes, and Python literals (`None`, `True`, `False`) to give you valid, strict JSON. Perfect for cleaning up logs, config files, and copy-pasted debugging data that strict parsers reject.
Why Python Data isn't JSON
Many developers assume Python dictionaries and JSON are identical. While they look similar, you cannot simply copy-paste a Python dict into a JSON parser.
They have critical syntax differences:
-
Quotes
{'key': 'value'}
Python accepts single quotes. JSON must use double quotes.
-
Booleans
True / False
Python uses capitalized
True. JSON uses lowercasetrue. -
Null Type
None
Python uses
None. JSON usesnull. -
Trailing Commas
{"a": 1,}
Python allows trailing commas. JSON will throw a syntax error.
How to handle JSON in Python
1. Convert Dict to JSON String
json.dumps()Use the built-in json library to serialize Python objects.
import json
data = {
'name': 'Alice',
'active': True,
'score': None
}
# Convert to JSON string
json_str = json.dumps(data)
print(json_str)
# Output: {"name": "Alice", "active": true, "score": null}
2. Pretty Print JSON
indent=4To make your output readable (like this tool does), use the indent parameter.
print(json.dumps(data, indent=4, sort_keys=True))
๐ฅ Common Error: TypeError not JSON serializable
You might encounter this error if you try to dump non-standard objects like set() or custom classes.
Fix: Convert sets to lists before dumping.
data['tags'] = list(my_set_of_tags)
4. Working with Files
json.dump / json.loadWrite to File
with open('data.json', 'w') as f:
json.dump(data, f, indent=4)
Use json.dump (no 's') to write directly to a file.
Read from File
with open('data.json', 'r') as f:
data = json.load(f)
Use json.load to parse a file into a Python dict.
๐ผ Pandas to JSON
Data scientist? Convert DataFrames instantly.
df.to_json('data.json', orient='records')
๐ API JSON Response
Fetching data with requests?
response = requests.get('https://api...')
data = response.json() # Dict
๐ฅ Handling Dates & Custom Objects
default=strBy default, datetime objects throw errors. Use `default=str` to fix it quickly.
import json
from datetime import datetime
data = {'timestamp': datetime.now()}
# Fixes "Object of type datetime is not JSON serializable"
print(json.dumps(data, default=str))
# Output: {"timestamp": "2023-10-27 10:00:00.123456"}
๐ป CLI Pretty Print
Format JSON file directly from terminal.
cat data.json | python -m json.tool
๐ก๏ธ Safe Parsing
Parsing untrusted Python strings? Use ast.
import ast
# Safer than eval()
data = ast.literal_eval("{'a': 1}")
Need to format standard JSON files?
Our advanced JSON Formatter handles large files, validation, and pretty-printing.
โจ Go to JSON Formatter โ