-
-
Save vkolev/1699497 to your computer and use it in GitHub Desktop.
Script to prettyprint and highlight json to terminal
This file contains 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/env python | |
import cStringIO | |
import json | |
import sys | |
from pygments import highlight | |
from pygments.formatters import Terminal256Formatter | |
from pygments.lexers.web import JavascriptLexer | |
from pygments import styles | |
def main(): | |
pretty_input = cStringIO.StringIO() | |
if len(sys.argv) == 1: | |
infile = sys.stdin | |
elif len(sys.argv) == 2: | |
infile = open(sys.argv[1], 'rb') | |
else: | |
raise SystemExit("{0} [infile [outfile]]".format(sys.argv[0])) | |
try: | |
obj = json.load(infile) | |
except ValueError, e: | |
raise SystemExit(e) | |
json.dump(obj, pretty_input, sort_keys=True, indent=4) | |
pretty_input.write('\n') | |
monokai = styles.get_style_by_name("monokai") | |
highlight(pretty_input.getvalue(), JavascriptLexer(), | |
Terminal256Formatter(style=monokai), sys.stdout) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment