Skip to content

Instantly share code, notes, and snippets.

@chales
Last active August 12, 2023 09:03
Show Gist options
  • Star 17 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)"
@michaelmaass
Copy link

@erikvandorp To add to levigroker ... just make sure your file bbedit-pretty-json.sh contains exactly the lines provided further up. Here is a screenshot illustrating how the file is supposed to look:

bbedit-pretty-json_sh

@erikvandorp
Copy link

@erikvandorp check that your JSON is valid. https://jsonlint.com/

Tnx levigroker, it seems not to be valid json. Will have this checked.

@erikvandorp
Copy link

@erikvandorp To add to levigroker ... just make sure your file bbedit-pretty-json.sh contains exactly the lines provided further up. Here is a screenshot illustrating how the file is supposed to look:

bbedit-pretty-json_sh

Tnx for reminding me michaelmaass, it is identical.

@erikvandorp
Copy link

tnx people for the quick reply. stay healthy!

@michaelmaass
Copy link

michaelmaass commented Mar 19, 2020

@erikvandorp The error message you quoted starts with "line 1" and then quotes what should be in line 2. Open the file in BBedit, click on the small cog at the top left, tick Display > [√] Schow invisibles > [√] Show spaces, make a screenshot, and post it here. Maybe I can see what causes the problem.

@erikvandorp
Copy link

Screenshot 2020-03-19 at 17 39 13

@michaelmaass
Copy link

@erikvandorp The file looks perfectly fine. Last attempt: Attach your file here. I can offer to try whether it works in my installation with the same BBedit 12.6.7 — but in macOS Mojave. Should there be no error, your problem is presumably related to macOS Catalina. It could be about the version of Python, for instance: https://developer.apple.com/documentation/macos_release_notes/macos_catalina_10_15_release_notes#3318248. Maybe your macOS defaults to python3 and the syntax is different. Mind that this is just a guess. Unfortunately Python is not my area of expertise at all.

@erikvandorp
Copy link

I would if I could, but it contains customer sensitive data. Tnx for your offer, though. Will check out the python issue.

@michaelmaass
Copy link

If your file really contains customer sensitive data that might be the cause of the problem. ;-)
I was referring to bbedit-pretty-json.sh, not to the JSON file you want to prettify, of course. It should not contain anything other than these 2 lines:

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

@erikvandorp
Copy link

Hey Michael, it does not contain anything other than those 2 lines. I am pretty sure it is an error inside the json file itself.

@erikvandorp
Copy link

tnx for the support, but I will be ok. Enjoy your Friday!

@jipexu
Copy link

jipexu commented Jun 2, 2020

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

@PioneerSand
Copy link

Thank you! This is a big time saver

@maccer83
Copy link

maccer83 commented Nov 24, 2020

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))"

@d1dsy08
Copy link

d1dsy08 commented Feb 9, 2021

Works perfect. Thanks!

Used on macOS 10.15.7 / Python 3.9 / BBEdit 13.5.4

@hsoi
Copy link

hsoi commented Mar 31, 2021

This was great! macOS 11.2.3, BBEdit 13.5.5, Python 2.7.16

@utdrmac
Copy link

utdrmac commented Apr 15, 2021

This script did not like my custom bash functions, so I modified it to call python directly:

#!/usr/bin/env python3
import sys, json
print(json.dumps(json.load(sys.stdin), indent=2))

Be sure to chmod +x the script. You don't have to restart BBedit.

@p44-Joe
Copy link

p44-Joe commented May 7, 2021

^^^ @utdrmac's post did the trick for me.

@klerafukan
Copy link

@maccer83 Thank you. That was the solution for me.

@mikepictor
Copy link

I can't get this to work at all, and I tried all the variations posted above. My test json is simply
{test:'test'}
and an example of the error I get

================================================================================
May 26, 2021 at 5:21:50 PM
~/Library/Application Support/BBEdit/Text Filters/FormatJSON.sh
--------------------------------------------------------------------------------
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 291, in load
    **kw)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

@levigroker
Copy link

levigroker commented May 26, 2021

{test:'test'} is not valid JSON. Sanity check using https://jsonlint.com
Consider {"test":"test"} instead.

@mikepictor
Copy link

mikepictor commented May 26, 2021 via email

@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