23

This is a follow-up question to this one. I need to both prettyprint a python dict (so I can visually inspect/modify it), and I need to have the output in json format (with the double quotes).

pprint module prettyprints the dict but uses single quotes (not json!). That's the topic of the linked other question/answer.

json.dumps will use the double quotes, but prints it in a big line (not human readable!)

How can we achieve both?

Community
  • 1
  • 1
travelingbones
  • 7,919
  • 6
  • 36
  • 43

1 Answers1

39

See the docs:

import json

print(json.dumps(
    {'4': 5, '6': 7},
    sort_keys=True,
    indent=4,
    separators=(',', ': ')
))
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Jérôme
  • 13,328
  • 7
  • 56
  • 106