Created
December 7, 2010 20:39
-
-
Save expandrive/732368 to your computer and use it in GitHub Desktop.
Take JSON from stdin and dump it to stdout in a more readable form
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/python | |
# From http://stackoverflow.com/questions/352098/how-to-pretty-print-json-script | |
# To install paste this into a file named prettyjson that lives in your PATH | |
# e.g. | |
# sudo touch /usr/bin/prettyjson | |
# sudo open -t /usr/bin/prettyjson | |
# paste this file and save | |
# sudo chmod +x /usr/bin/prettyjson | |
""" | |
Convert JSON data to human-readable form. | |
Usage: | |
prettyJSON.py inputFile [outputFile] | |
""" | |
import sys | |
import simplejson as json | |
def main(args): | |
try: | |
inputFile = open(args[1]) | |
input = json.load(inputFile) | |
inputFile.close() | |
except IndexError: | |
usage() | |
return False | |
if len(args) < 3: | |
print json.dumps(input, sort_keys = False, indent = 4) | |
else: | |
outputFile = open(args[2], "w") | |
json.dump(input, outputFile, sort_keys = False, indent = 4) | |
outputFile.close() | |
return True | |
def usage(): | |
print __doc__ | |
if __name__ == "__main__": | |
sys.exit(not main(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment