33

In python, If I have a JSON object obj, then I can

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

in order to get a pretty printout of the object. Is it possible to prettify the output even further: add some colors in particular? Something like the result of [1]

cat foo.json | jq '.'

[1] jq the JSON Swiss Army toolbox: http://stedolan.github.io/jq/

Dror
  • 11,068
  • 19
  • 81
  • 145
  • What is `jq`? What does it do? –  Sep 03 '14 at 07:45
  • Maybe [ipython](http://ipython.org/) has something for this, buy i don't know exactly – stalk Sep 03 '14 at 07:52
  • @Tichodroma: Added a reference – Dror Sep 03 '14 at 07:53
  • @stalk: I want it to be integrated in the script, such that the output to the console will be colored. – Dror Sep 03 '14 at 07:54
  • 2
    It is definitely possible (you can just call this `jq` as a subprocess from Python) but if you are asking: "is there a way to do that with the built-in json library" then the answer is "no". – freakish Sep 03 '14 at 08:00
  • @freakish: I could live with calling `jq`. Can you explain how can I do it? – Dror Sep 03 '14 at 08:05
  • @Dror Actually I was wrong. You can't do this with subprocess, see this: http://stackoverflow.com/questions/13299550/preserve-colored-output-from-python-os-popen – freakish Sep 03 '14 at 08:39
  • @Dror I think that what you are left with is to either search for a library that supports this or write it on your own. – freakish Sep 03 '14 at 08:43

4 Answers4

57

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

arnuschky
  • 2,099
  • 1
  • 19
  • 15
7

The accepted answer doesn't seem to be working with more recent versions of Pygments and Python. So here's how you can do it in Pygments 2.7.2+:

import json
from pygments import highlight
from pygments.formatters.terminal256 import Terminal256Formatter
from pygments.lexers.web import JsonLexer

d = {"test": [1, 2, 3, 4], "hello": "world"}

# Generate JSON
raw_json = json.dumps(d, indent=4)

# Colorize it
colorful = highlight(
    raw_json,
    lexer=JsonLexer(),
    formatter=Terminal256Formatter(),
)

# Print to console
print(colorful)
Haterind
  • 505
  • 3
  • 12
2

I like using rich which has a dependency on pyments, but it covers all your console coloring needs, it also autoformats json: enter image description here

MortenB
  • 1,874
  • 19
  • 28
0

For python3 :

#!/usr/bin/python3
#coding: utf-8

from pygments import highlight, lexers, formatters
import json

d = {"test": [1, 2, 3, 4], "hello": "world"}

formatted_json = json.dumps(d, indent=4)
colorful_json = highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter())
print(colorful_json)
Olivier Lasne
  • 529
  • 4
  • 11