๐Ÿ›ก๏ธ Safe Web Utils

๐Ÿ

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.

Secure Client-Side

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 lowercase true.

  • Null Type
    None

    Python uses None. JSON uses null.

  • 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=4

To 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.

TypeError: Object of type set is not JSON serializable

Fix: Convert sets to lists before dumping.

data['tags'] = list(my_set_of_tags)

4. Working with Files

json.dump / json.load

Write 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=str

By 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 โ†’

Explore More Tools