Skip to content

Instantly share code, notes, and snippets.

@inconvergent
Last active March 2, 2018 13:45
Show Gist options
  • Save inconvergent/f05bbe982a4859eb8c369d15ff8accc8 to your computer and use it in GitHub Desktop.
Save inconvergent/f05bbe982a4859eb8c369d15ff8accc8 to your computer and use it in GitHub Desktop.
show colorful json, regular json or just the text from stdin
#!/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