Skip to content

Instantly share code, notes, and snippets.

@brokaw
Last active February 2, 2024 04:29
Show Gist options
  • Save brokaw/95ade1358954cd97d0f2c8e992e14b08 to your computer and use it in GitHub Desktop.
Save brokaw/95ade1358954cd97d0f2c8e992e14b08 to your computer and use it in GitHub Desktop.
A BBEdit text filter to prettify JSON.
#!/usr/bin/env python3
# A text filter for BBEdit. If it encounters a JSON error, it writes an error message
# to stderr (appears in a new BBEdit window) and leaves the original text unaltered.
# c.f. http://crisp.tumblr.com/post/2574967567/json-pretty-print-formatting-in-bbedit
# c.f. http://blog.scottlowe.org/2013/11/11/making-json-output-more-readable-with-bbedit/
import json
import sys
def main():
input = sys.stdin.read()
try:
obj = json.loads(input)
except Exception as e:
sys.stderr.write("Error reading JSON: %s" % str(e))
return 1
json.dump(obj, sys.stdout, ensure_ascii=True, indent=2)
return 0
if __name__ == '__main__':
sys.exit(main())
@doogiecode
Copy link

This is a delightfully tiny way to do a similar thing for people who have the jq utility installed. :) (Bonus: using jq -c instead does a minify.)

#!/bin/sh

jq

@nrrkeene
Copy link

nrrkeene commented Dec 8, 2022

This worked for me without modification, thank you.

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