-
-
Save mjclemente/419452f3e2916c24517a1773b3922f5f to your computer and use it in GitHub Desktop.
A BBEdit text filter to prettify JSON.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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()) |
In BBEdit 15.1.1 (15B26, 64-bit Intel), this text filter appears to not be UTF-8 aware.
This json object in the source: "pris_før_moms":10680 becomes: "pris_f\u00f8r_moms": 10680
My solution is to change to "ensure_ascii=False" in line 19:
json.dump(obj, sys.stdout, ensure_ascii=False, indent=2)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In BBEdit 15.1.1 (15B26, 64-bit Intel), this text filter appears to not be UTF-8 aware.
This json object in the source: "pris_før_moms":10680
becomes: "pris_f\u00f8r_moms": 10680