TextWrangler filters to tidy XML and tidy JSON

I work with XML and JSON a lot, often as the input to or output from web services. Generally it is unformatted, so before I can read the data I need it formatted and whitespaced. So here are some TextWrangler filters to tidy up XML and JSON documents.

#!/bin/sh
XMLLINT_INDENT=$'\t' xmllint --format --encode utf-8 -

Save this into a file called Tidy XML.sh

#!/usr/bin/python
import fileinput
import json
print json.dumps( json.loads(''.join([line.strip() for line in fileinput.input()])), sort_keys=True, indent=2)

Save this into a file called Tidy JSON.py

Drop these into ~/Library/Application Support/TextWrangler/Text Filters. You can then run them on a file within TextWrangler by choosing Text > Apply Text Filter > [filter].

Advertisements