Tidy JSON Formatting with TextWrangler

This script can be used to format JSON. Place the script in your TextWrangler Text Filters folder. For me the path was /Library/Application Support/TextWrangler/Text Filters. You can name the script whatever you want, just make sure to give it a .py extension. I called mine TidyJSON.py.


import fileinput
import json

if name == "main":
jsonStr = ''
for aline in fileinput.input():
jsonStr = jsonStr + ' ' + aline.strip()
jsonObj = json.loads(jsonStr)
print json.dumps(jsonObj, sort_keys=True, indent=2)

After saving the file, restart TextWrangler and go to Text -> Apply Text Filter -> TidyJSON. Your garbage-looking JSON will look like a million bucks!