Skip to content

Instantly share code, notes, and snippets.

@guyarad
Last active September 14, 2016 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guyarad/d80b9d5652a6616e1c3138b6a86310c4 to your computer and use it in GitHub Desktop.
Save guyarad/d80b9d5652a6616e1c3138b6a86310c4 to your computer and use it in GitHub Desktop.
Text filter intended to be used with TextWrangler application (Mac OSX) in order to reformat (tidy-up) a JSON file.
#!/usr/bin/python
#
# Script was modified from:
# https://steveswinsburg.wordpress.com/2014/09/26/textwrangler-filters-to-tidy-xml-and-tidy-json/
# Modified by Guy Arad, and can be found in:
# https://gist.github.com/guyarad/d80b9d5652a6616e1c3138b6a86310c4
#
# Installation:
# Copy to "~/Library/Application Support/TextWrangler/Text Filters"
#
# Usage:
# Within TextWrangler, Menu -> Text -> Apply Text Filter -> [Filter Name]
#
import fileinput
import json
import itertools
input1, input2 = itertools.tee(fileinput.input())
try:
print json.dumps(
json.loads(''.join(line.strip() for line in input1)),
sort_keys=True,
indent=2)
finally:
print ''.join(input2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment