Simple BBEdit Text Filter that’s been useful: PrettyJSON.py

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!

Advertisement

5 thoughts on “Simple BBEdit Text Filter that’s been useful: PrettyJSON.py”

  1. 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.

    Rick

    1. Hmm. Not sure. Do you have the #!/usr/bin/env python line 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.

  2. 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!

    Rick

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: