Python: Pretty Printing JSON

Have you ever wanted to output a JSON object to the console. Its actually rather easy to do.

Make sure you import json at the top of your file

print json.dumps({"name": "Leland", "profession": "programmer"}, sort_keys=True, indent=4, separators=(',', ': '))

This will output

{
    "name": "Leland",
    "profession": "programmer"
}