Created
July 6, 2012 13:40
-
-
Save husio/3060215 to your computer and use it in GitHub Desktop.
Pretty print of any JSON
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 sys | |
| import json | |
| import pprint | |
| import urllib | |
| def main(): | |
| if len(sys.argv) == 2: | |
| data = urllib.urlopen(sys.argv[1]).read() | |
| else: | |
| chunks = [] | |
| while True: | |
| in_ = sys.stdin.read() | |
| if in_: | |
| chunks.append(in_) | |
| else: | |
| break | |
| data = "".join(chunks) | |
| try: | |
| data = json.loads(data) | |
| except ValueError: | |
| sys.exit("Cannot encode json: {}".format(data[:100])) | |
| try: | |
| from pygments import highlight | |
| from pygments.lexers import JavascriptLexer | |
| from pygments.formatters import TerminalFormatter | |
| print highlight(pprint.pformat(data), JavascriptLexer(), TerminalFormatter()) | |
| except ImportError: | |
| pprint.pprint(data) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment