Skip to content

Instantly share code, notes, and snippets.

@dblackdblack
Created September 6, 2013 23:55
Show Gist options
  • Save dblackdblack/6471481 to your computer and use it in GitHub Desktop.
Save dblackdblack/6471481 to your computer and use it in GitHub Desktop.
formatjson.py
#!/usr/bin/env python
import fileinput
import json
import re
if __name__ == "__main__":
jsonStr = ''
for a_line in fileinput.input():
jsonStr = jsonStr + ' ' + a_line.strip()
# strip any trailing semicolon so we can also format object literal statements
p = re.compile( ';\s*$' )
jsonObj = json.loads(p.sub( '', jsonStr ))
print json.dumps(jsonObj, sort_keys=True, indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment