Pretty-printing JSON with ASCII color in python

10,082

Solution 1

Use Pygments library:

import json
from pygments import highlight
from pygments.lexers import JsonLexer
from pygments.formatters import TerminalFormatter

json_object = json.loads('{"foo":"bar"}')
json_str = json.dumps(json_object, indent=4, sort_keys=True)
print(highlight(json_str, JsonLexer(), TerminalFormatter()))

Solution 2

Use https://github.com/willmcgugan/rich

from rich.console import Console
from rich.theme import Theme
from rich.highlighter import RegexHighlighter, _combine_regex
my_theme = Theme(
    {
        "repr.str": "bright_blue",
        "repr.value_str": "green",
    }
)

class ReprHighlighter(RegexHighlighter):
    """Highlights the text typically produced from ``__repr__`` methods."""

    base_style = "repr."
    highlights = [
        r"(?P<tag_start>\<)(?P<tag_name>[\w\-\.\:]*)(?P<tag_contents>[\w\W]*?)(?P<tag_end>\>)",
        r"(?P<attrib_name>[\w_]{1,50})=(?P<attrib_value>\"?[\w_]+\"?)?",
        r"(?P<brace>[\{\[\(\)\]\}])",
        _combine_regex(
            r"(?P<ipv4>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})",
            r"(?P<ipv6>([A-Fa-f0-9]{1,4}::?){1,7}[A-Fa-f0-9]{1,4})",
            r"(?P<eui64>(?:[0-9A-Fa-f]{1,2}-){7}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{1,2}:){7}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{4}\.){3}[0-9A-Fa-f]{4})",
            r"(?P<eui48>(?:[0-9A-Fa-f]{1,2}-){5}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{1,2}:){5}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})",
            r"(?P<call>[\w\.]*?)\(",
            r"\b(?P<bool_true>True)\b|\b(?P<bool_false>False)\b|\b(?P<none>None)\b",
            r"(?P<ellipsis>\.\.\.)",
            r"(?P<number>(?<!\w)\-?[0-9]+\.?[0-9]*(e[\-\+]?\d+?)?\b|0x[0-9a-fA-F]*)",
            r"(?P<path>\B(\/[\w\.\-\_\+]+)*\/)(?P<filename>[\w\.\-\_\+]*)?",
            r":(?<![\\\w]) (?P<value_str>b?\'\'\'.*?(?<!\\)\'\'\'|b?\'.*?(?<!\\)\'|b?\"\"\".*?(?<!\\)\"\"\"|b?\".*?(?<!\\)\")",
            r"(?<![\\\w])(?P<str>b?\'\'\'.*?(?<!\\)\'\'\'|b?\'.*?(?<!\\)\'|b?\"\"\".*?(?<!\\)\"\"\"|b?\".*?(?<!\\)\")",
            r"(?P<uuid>[a-fA-F0-9]{8}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{12})",
            r"(?P<url>(https|http|ws|wss):\/\/[0-9a-zA-Z\$\-\_\+\!`\(\)\,\.\?\/\;\:\&\=\%\#]*)",
        ),
    ]


console = Console(theme=my_theme, highlighter=ReprHighlighter())
json_str = json.dumps(json_object, indent=4, sort_keys=True)
console.print(json_str)

only value_str was added into ReprHighlighter

Solution 3

This should get you started (it prints keys in blue):

import json
import urllib2

# ANSI color terminal escape sequences
OKBLUE = '\033[94m'
ENDC = '\033[0m'

def pretty(keyvals, indent=''):
    print '{'
    for key, val in keyvals.iteritems():
        print '{}  {}"{}"{}:'.format(indent, OKBLUE, key, ENDC),
        if isinstance(val, dict):
            pretty(val, indent + '  ')
        elif isinstance(val, str):
            print '"{}",'.format(val)
        else:
            print '{},'.format(val)
    print indent + '},'

req = urllib2.Request('http://coinabul.com/api.php', headers={
        'User-Agent': 'Mozilla/5.0',
        })

page = urllib2.urlopen(req)
parsed = json.load(page)

pretty(parsed)
Share:
10,082
Robert T. McGibbon
Author by

Robert T. McGibbon

Updated on June 08, 2022

Comments

  • Robert T. McGibbon
    Robert T. McGibbon about 2 years

    I'm looking to print JSON to the command line, in python, with ASCII colors. For example, the (excellent) jq utility will color-ify JSON using bold ASCII colors like so:

    • input: curl --silent http://coinabul.com/api.php | jq .
    • output:
      jq output

    Does anyone know how to accomplish this effect from Python? A couple of SO questions provide some good information on using ASCII colors from python (e.g. Print in terminal with colors using Python?), but this effect requires combining the pretty-printing machinery with the colorify-ing machinery in a different way, I think.

  • Robert T. McGibbon
    Robert T. McGibbon over 9 years
    It would be nice if it were possible to avoid re-inventing all of the pretty-printing (indenting) logic.
  • John Zwinck
    John Zwinck over 9 years
    Well, you could use json.dumps(parsed, indent=2) and then parse the text it gives you and colorize that...I thought that was worse (more error prone, etc.). If what you're looking for is a library that does all this for you, well, I think it doesn't exist.
  • PhilLab
    PhilLab about 6 years
    I had to import like this: from pygments.lexers.data import JsonLexer from pygments.formatters.terminal import TerminalFormatter
  • Santhosh
    Santhosh almost 5 years
    TerminalFormatter() has option for style also, like TerminalFormatter(style='monokia'). How to see the list of all style names so that i can try them
  • kerma
    kerma almost 5 years