Pretty-printing JSON with ASCII color in python
Asked Answered
J

3

17

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.

Jemie answered 20/10, 2014 at 6:38 Comment(0)
V
34

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()))
Vivianaviviane answered 14/7, 2016 at 11:1 Comment(3)
I had to import like this: from pygments.lexers.data import JsonLexer from pygments.formatters.terminal import TerminalFormatterNicky
TerminalFormatter() has option for style also, like TerminalFormatter(style='monokia'). How to see the list of all style names so that i can try themFabrication
@SanthoshYedidi see here: bitbucket.org/birkenfeld/pygments-main/src/…Vivianaviviane
A
3

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

Airing answered 30/6, 2021 at 7:30 Comment(0)
F
2

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)
Flower answered 20/10, 2014 at 7:15 Comment(3)
It would be nice if it were possible to avoid re-inventing all of the pretty-printing (indenting) logic.Jemie
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.Flower
I think this is useful if you want something simple and you don't want to import any non-standard library and have it working out-the-box...Hazzard

© 2022 - 2024 — McMap. All rights reserved.