Dad’s been working with some JSON output from a web service lately and it comes back lacking line-feeds and indentation which is great for transmission but hard to read for humans. Dad’s not a python programmer and Geek’s busy writing a Humanities 110 paper, but the simple python script
PrettyJSON.py (below) seems to work if you put it into
~/Library/Application Support/BBEdit/Text Filters/
It can be used to beautify the front window in BBEdit if that contains JSON:
#!/usr/bin/env python import fileinput import json if __name__ == "__main__": jsonStr = '' for a_line in fileinput.input(): jsonStr = jsonStr + ' ' + a_line.strip() jsonObj = json.loads(jsonStr) print json.dumps(jsonObj, sort_keys=True, indent=2)
~/Library/Application Support/TextWrangler/Text Filters/
instead, as one would expect.
This gives you a new item in the “Text” -> “Apply Text Filter” sub-menu named whatever you named the file (PrettyJSON.py in my case). Open a messy JSON file, select “Text” -> “Apply Text Filter” -> “PrettyJSON.py” and watch your file contents magically become beautiful! Thanks BBEdit!