Skip to content

Instantly share code, notes, and snippets.

@chales
Last active April 26, 2024 20:55
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chales/510c5bed59a32ed0cb55 to your computer and use it in GitHub Desktop.
Save chales/510c5bed59a32ed0cb55 to your computer and use it in GitHub Desktop.
Make JSON legible with BBEdit
2016/03/15
Tested with BBEdit 11.5 / OS X 10.11.3 / Python 2.7.10
http://grokin.gs/blog/elegant-json-pretty-print-for-bbedit/
Place the script (or a link to this script) in the ~/Library/Application Support/BBEdit/Text Filters directory
Restart BBEdit.
The new filter should be under: "Test > Apply Text Filter > bbedit-pretty-json"
#!/bin/bash
python -c "import sys, json; print json.dumps(json.load(sys.stdin), indent=2)"
@venksv
Copy link

venksv commented Aug 9, 2021

Hi
if i follow this with a valid (geojson file) i got this BBedit error
application error code: 100032

catalina 10.15.4
BBedit 13.0.6

Any advice
Thanks

Hi - if you're python version is post 3.), the print syntax changes to a function. So you need to encapsulate the "json.dumps ..." in parentheses like so:
print(json.dumps(json.load(sys.stdin), indent=2))

I got the same error and fixed it with this.

@venksv
Copy link

venksv commented Aug 9, 2021

First off - many thanks for this bit of magic. Majorly helpful while editing in BBEdit. One thing I wanted to mention: Folks may want to determine the right folder by going BBEdit --> Folders --> Text Filters. This opens up Finder at that location.

For some reason, on my Mac (BigSur 11.5, BBEdit v14.0), it was in some convoluted location (~/Library/Containers/com.barebones.bbedit/Data/Library/Application Support/BBEdit/Text Filters) and not the regular ~/Library/Application Support/BBEdit/Text Filters

@jipexu
Copy link

jipexu commented Aug 9, 2021

Hi
if i follow this with a valid (geojson file) i got this BBedit error
application error code: 100032
catalina 10.15.4
BBedit 13.0.6
Any advice
Thanks

Hi - if you're python version is post 3.), the print syntax changes to a function. So you need to encapsulate the "json.dumps ..." in parentheses like so:
print(json.dumps(json.load(sys.stdin), indent=2))

I got the same error and fixed it with this.

@venksv thank for the tip !

@jodischneider
Copy link

@venksv thanks again for the tip! Updating to print(json.dumps(json.load(sys.stdin), indent=2)) for Python 3.x worked for me.

@wdumaine
Copy link

wdumaine commented Jan 12, 2023

Thank you all for the tip for Python 3. It works great on Apple M1 with Z shell (zsh) as the default shell.

Tested with BBEdit 14.6.2, macOS 12.6.2 and Python 3.11.1

#!/bin/zsh
python3 -c "import sys, json; print (json.dumps(json.load(sys.stdin), indent=2))"

@klwdallas
Copy link

First off - many thanks for this bit of magic. Majorly helpful while editing in BBEdit. One thing I wanted to mention: Folks may want to determine the right folder by going BBEdit --> Folders --> Text Filters. This opens up Finder at that location.

For some reason, on my Mac (BigSur 11.5, BBEdit v14.0), it was in some convoluted location (~/Library/Containers/com.barebones.bbedit/Data/Library/Application Support/BBEdit/Text Filters) and not the regular ~/Library/Application Support/BBEdit/Text Filters

Me too. Thank you!

@klwdallas
Copy link

Hey! for me it did not work. I get the following message:
File "", line 1
import sys, json; print json.dumps(json.load(sys.stdin), indent=2)
^
SyntaxError: invalid syntax
Any ideas? (I use mac os Catalina 10.15.3 with BBedit 12.6.7)

Hi! As @michaelmaass says, Catalina uses Python 3 as standard instead of Python 2. Just did a quick google, and one of the changes is that the print statement has changed to a function and thus requires parentheses, which should also work fine in Python 2. So if you change line 2 to the following, it works: python -c "import sys, json; print(json.dumps(json.load(sys.stdin), indent=2))"

@maccer83 - Thank you. This fixed it for me.

@michaldobisek
Copy link

If you have jq installed (which could be simply done via homebrew brew install jq), then the Format JSON filter could be as simple as

#!/bin/bash
jq .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment