Coloring JSON output in python

You can use Pygments to color your JSON output. Based on what you have:

formatted_json = json.dumps(obj, sort_keys=True, indent=4)

from pygments import highlight, lexers, formatters
colorful_json = highlight(unicode(formatted_json, 'UTF-8'), lexers.JsonLexer(), formatters.TerminalFormatter())
print(colorful_json)

Output example:

Output example of pygments colored code

Leave a Comment