Skip to content

Instantly share code, notes, and snippets.

@brokaw
Last active February 2, 2024 04:29
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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())
@mdahlman
Copy link

mdahlman commented Feb 8, 2019

First:
This script was a big help! Thanks so much.

Second:
I was not able to run it as I found it. I needed to change the first line to this:
#!/usr/bin/env python

I see that you had that in the past. So it was removed intentionally... but I don't understand why.

Third:
I had loads of problems like this: Expecting property name: line 1 column 2 (char 1)
I was able to fix it by importing and using "ast" with the information from this answer: https://stackoverflow.com/a/36599122/8373

I guess I could post my variation somewhere else... but I'm looking for any comments about whether adding "ast" has drawbacks and why you changed that first line.

Thanks again!

@brokaw
Copy link
Author

brokaw commented Apr 10, 2019

@mdahlman Glad this helped you. You're right about the shebang line being messed up. That must have been an inadvertent change made during my last commit. Technically, JSON only supports double quotes. Loading the ast module feels pretty heavy-weight to me; I would probably do a character substitution instead. But I don't run into files like that, so YMMV.

@stancel
Copy link

stancel commented Jul 7, 2019

Worked like a charm. Thanks!

@MonkandMonkey
Copy link

Support unicode character output, instead of \u6690

#!/usr/bin/env python
# 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
    sys.stdout.write(json.dumps(obj, ensure_ascii=False, indent=2))
    return 0

if __name__ == '__main__':
    sys.exit(main())

@febalci
Copy link

febalci commented Oct 26, 2019

Same problem (Third one) with @mdahlman. Again solved with ast:

#!/usr/bin/env python
# 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
import ast

def main():
    input = sys.stdin.read()
    try:
        obj = ast.literal_eval(input)
#        obj = json.loads(input)
    except Exception as e:
        sys.stderr.write("Error reading JSON: %s" % str(e))
        return 1
    sys.stdout.write(json.dumps(obj, indent=2))
    return 0

if __name__ == '__main__':
    sys.exit(main())

Thanks to both of you; @brokaw and @mdahlman...

@selimnairb
Copy link

Slight update and targets Python 3 (since 2 is now EOL and built-in Python 2 on Big Sur warns to use Python 3 instead):

#!/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(f"Error reading JSON: {str(e)}\n")
        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