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)
Because Bare Bones Software is so cool this works with their free Text Wrangler editor as well except you just put the files in
~/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!
5 thoughts on “Simple BBEdit Text Filter that’s been useful: PrettyJSON.py”
Put PrettyJSSON.py in:
~/Library/Application Support/BBEdit/Text Filters
but it doesn’t show up under BBEdit v 10.5.13 Text->Apply Text Filter.
Nothing shows up and I have put three different version of the JSON filter there.
Any idea what i might be doing wrong? I restarted BBEdit. I tried chmod +x the files.
Appreciate any help.
Hmm. Not sure. Do you have the
#!/usr/bin/env pythonline in there so BBEdit knows it’s executable?
Also, check the BBEdit Preferences in the Menus & Shortcuts section and make sure that all the items of Text -> Apply Text Filter -> have the check mark next to them so they’re enabled.
After that I’m afraid I’d pass you to Bare Bones tech support. They’re very helpful.
I had moved BBEdit Application Support folder under ~/Dropbox but was putting the filters under ~Library. BBEdit support helped me super fast!
Thanks to you, too!
glad you got it sorted out. Yah – great support from a great small business.