JSON Pretty Print formatting in BBEdit
Working with large JSON output can be a pill, especially when the server strips white-space or makes no effort at formatting the response for human-readability.
Having previously looked for a solution to make this easy, and not finding the right Google-majik to find it, I came across this article that talks about BBEdit’s Unix filtering abilities: BBEdit: Its Unix Support Doesn’t Suck Either, Part 2
I created this Python script that applies JSON pretty-print to text selected in the editor window:
#!/usr/local/bin/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)
- Create the script (name it what you will, I named mine “PrettyJSON.py”), and save it in the “Unix Filters” folder for BBEdit (as described in the above article.
- Copy some JSON into BBEdit, select it, and apply the filter.
Voila! Human-readable JSON.
UPDATE: If you are using BBEdit 10…
- The ”PrettyJSON.py” script should be placed here: “~/Library/Application Support/BBEdit/Text Filters”
- Run the filter using the menu sequence: Text->Apply Text Filter->PrettyJSON
[NOTE: Also works on TextWrangler, the free, light version of BBEdit.]