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

Expected output

{
  "items": [1, 2, 3],
  "name": "Bob"
}

Related snippets

Related DuskTools