Created
November 10, 2012 07:21
-
-
Save Leechael/4050286 to your computer and use it in GitHub Desktop.
Pretty print for JSON, in Python
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 | |
# -*- coding: utf8 -*- | |
from simplejson import loads | |
from simplejson.decoder import JSONDecodeError | |
from betterprint import pprint | |
from os import path | |
if __name__ == '__main__': | |
from sys import argv, exit, stdin | |
import fileinput | |
if len(argv) < 2 and stdin.isatty(): | |
print "Usage: %s [json_file]" % argv[0] | |
exit(1) | |
text = "\n".join((l for l in fileinput.input())) | |
try: | |
pprint(loads(text)) | |
except JSONDecodeError: | |
pprint(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment