-
-
Save chales/510c5bed59a32ed0cb55 to your computer and use it in GitHub Desktop.
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)" |
@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 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.
I would if I could, but it contains customer sensitive data. Tnx for your offer, though. Will check out the python issue.
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)"
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.
tnx for the support, but I will be ok. Enjoy your Friday!
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
Thank you! This is a big time saver
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 syntaxAny 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))"
Works perfect. Thanks!
Used on macOS 10.15.7 / Python 3.9 / BBEdit 13.5.4
This was great! macOS 11.2.3, BBEdit 13.5.5, Python 2.7.16
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.
^^^ @utdrmac's post did the trick for me.
@maccer83 Thank you. That was the solution for me.
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)
{test:'test'}
is not valid JSON. Sanity check using https://jsonlint.com
Consider {"test":"test"}
instead.
Hi
if i follow this with a valid (geojson file) i got this BBedit error
application error code: 100032catalina 10.15.4
BBedit 13.0.6Any 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.
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
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
ThanksHi - 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 !
@venksv thanks again for the tip! Updating to print(json.dumps(json.load(sys.stdin), indent=2)) for Python 3.x worked for me.
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))"
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!
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.
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 .
Environment:
macOS Sonoma 14.5 (23F79)
BBEdit version 15.0.3 (15A102, 64-bit Intel)
Using the current version some UTF-8 characters are returned encoded:
in: "forsøg"
out: "fors\u00f8g"
I have verified, that the BBEdit document is in fact using UTF-8 encoding. Any clues?
Environment: macOS Sonoma 14.5 (23F79) BBEdit version 15.0.3 (15A102, 64-bit Intel)
Using the current version some UTF-8 characters are returned encoded: in: "forsøg" out: "fors\u00f8g"
I have verified, that the BBEdit document is in fact using UTF-8 encoding. Any clues?
Solved: add ", ensure_ascii=False" to the json.dumps command:
#!/bin/bash
python3 -c "import sys, json; print (json.dumps(json.load(sys.stdin), indent=2, ensure_ascii=False))"
I concur with @michaldobisek in that jq is a much more elegant way to go, especially since python is no longer installed on macOS with the developer tools.
I've updated the gist I maintain with this approach: https://gist.github.com/levigroker/3777091
tnx people for the quick reply. stay healthy!