Pretty-Print JSON
Format JSON with indentation for readability.
Code
Python
import json
data = {"name": "Bob", "items": [1, 2, 3]}
pretty = json.dumps(data, indent=2, sort_keys=True)
print(pretty)Line-by-line explanation
- 1.json.dumps() serializes to a string.
- 2.indent=2 adds 2-space indentation.
- 3.sort_keys=True orders keys alphabetically.
Expected output
{
"items": [1, 2, 3],
"name": "Bob"
}