Skip to content

Instantly share code, notes, and snippets.

@pdelteil
Last active September 30, 2018 23:54
Show Gist options
  • Save pdelteil/45b8ce2c13956bd94a963681a976122c to your computer and use it in GitHub Desktop.
Save pdelteil/45b8ce2c13956bd94a963681a976122c to your computer and use it in GitHub Desktop.
#Convert JSON data to human-readable form.
#Usage:
# prettyJSON.py inputFile [outputFile]
import sys
import simplejson as json
def main(args):
try:
if args[1] == '-':
inputFile = sys.stdin
else:
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