BBEdit Text Filter to format JSON

First attempt at writing a BBEDit Text Filter

Created: by Pradeep Gowda Updated: Nov 04, 2023 Tagged: json · bbedit · python · code

I always wondered how some of the BBEdit features (like Text Filters) worked.

The model of operation is quite simple:

  • you have a script that takes input from the standard input (stdin) and run the program over that input.
  • you save this script (could be any language, I went with Python here.) under ~/Library/Application Support/BBEdit/Text Filters. I named the following script JSON Format.py; yes - space and all because that’s what shows up in BBEdit’s menu like this:

bbedit menu with this text filter{: width=“500”}

I open up a JSON file and run this option, to have it neatly formatted. Voilà!

#!/usr/bin/env python3
"""
JSON Format.py
This BBEdit TextF Filter formats a json file in the bbedit buffer
using python's json module

Pradeep Gowda
2022-10-23
"""

import json
import sys

text = sys.stdin.read()
print(json.dumps(json.loads(text), indent=2))

Commit the ~/Library/Application Support/BBEdit/ directory to git and you can use them from different mac machines running BBEdit:

cd ~/Library/Application Support/BBEdit/
git init
git push ..blah..

You may want to exclude the Notes.bbnotebookd directory which contains the “Notes”s.

See also: BBEdit, and jsonfmt