Created
July 13, 2010 18:43
-
-
Save edufelipe/474306 to your computer and use it in GitHub Desktop.
Script to prettyprint and highlight json to terminal
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/env python | |
import cStringIO | |
import json | |
import sys | |
from pygments import highlight | |
from pygments.formatters import TerminalFormatter | |
from pygments.lexers.web import JavascriptLexer | |
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') | |
highlight(pretty_input.getvalue(), JavascriptLexer(), | |
TerminalFormatter(), sys.stdout) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simple script to prettyprint json on a terminal.