Skip to content

Instantly share code, notes, and snippets.

@edufelipe
Created July 13, 2010 18:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edufelipe/474306 to your computer and use it in GitHub Desktop.
Save edufelipe/474306 to your computer and use it in GitHub Desktop.
Script to prettyprint and highlight json to terminal
#!/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()
@edufelipe
Copy link
Author

This is a simple script to prettyprint json on a terminal.

@siso
Copy link

siso commented Apr 2, 2014

Cool! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment