Last active
March 2, 2018 13:45
-
-
Save inconvergent/f05bbe982a4859eb8c369d15ff8accc8 to your computer and use it in GitHub Desktop.
show colorful json, regular json or just the text from stdin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
import sys | |
from json import loads | |
from json import dumps | |
try: | |
from pygments import highlight, lexers, formatters | |
def show(d): | |
print(highlight(dumps(loads(d), indent=2, sort_keys=True), | |
lexers.JsonLexer(), | |
formatters.TerminalFormatter())) | |
except Exception as e: | |
def show(d): | |
print(dumps(loads(d), indent=2, sort_keys=True)) | |
def main(d): | |
try: | |
print(show(d)) | |
except Exception as e: | |
s = '\njson error: ' + str(e) + '\n' | |
print(s, file=sys.stderr) | |
print(d) | |
if __name__ == '__main__': | |
d = '\n'.join([l for l in sys.stdin]) | |
main(d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment