Skip to content

Instantly share code, notes, and snippets.

@Leechael
Created November 10, 2012 07:21
Show Gist options
  • Save Leechael/4050286 to your computer and use it in GitHub Desktop.
Save Leechael/4050286 to your computer and use it in GitHub Desktop.
Pretty print for JSON, in Python
#!/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