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].
There’s a typo in the path for the text filters: the “l” is missing from TextWrangler.
Good catch, thanks, fixed!
Thanks, saved me some time!
This is really excellent. Thanks!
Thanks Dave. The guys use these at work too 🙂
I’ve actually noticed a minor bug in the XML filter – if the XML is declared as UTF-16 but only uses UTF-8 chars, it fails to parse. The fix is to just change the UTF-16 declaration to UTF-8, however I’d love to be able to cater for that scenario.
Exactly what I was looking for: thank you!
Sweet, thanks! Parsed a monster MRSS JSON file without a hiccup.
Osum thanks!!!
I’m probably doing something stupid, but the Tidy JSON generates an error: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py:383: ValueError: No JSON object could be decoded
The Tidy XML works great, but I know nothing about JSON.
Nevermind, it works on JSON examples from the web. Just not MY JSON for some reason. So there must be a missing } or , somewhere.
This is awesome! Thank you! Do you know how to do this for Java?
Have you heard of a tool named Eclipse? Try that.
I wouldn’t recommend Eclipse for this purpose as it involves creating a new file in a specific project, just so you can paste in the content and have it reformatted to look at, then remembering to clean up/remove the file afterwards. If you do this in TextWrangler, you can do it all in an ‘untitled text’ document with a simple Command+N keystroke.
I concur with Steve on this one. While I do use Eclipse to do actual development. It is often more convenient to have a Text Editor like TextWrangler to read existing code, especially when it is not source controlled.
Works like charm….Thanks a lot
The output changed order of the keys despite sort_key = true. Any clue on this? Is there a required version of Python? Thanks.
Steve, thank you for your comments! RE: My original question, do you know of a Tidy script for Java?
Hey Shannon, There are a couple of examples here:
http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java
and
http://stackoverflow.com/questions/8596161/json-string-tidy-formatter-for-java
cheers!
Actually, I am looking for a Java tidy script for TextWrangler. Are there any guides on how to build a new Tidy script for TextWrangler?
It’s just a shell script. Do you want it to tidy your Java code or do you want the filter to be written in Java? If the first, you’ll need a parser that can handle that. I would probably just use Eclipse and a decent preferences imported in, to tidy it all up. If the latter, use the examples from earlier, get it running, then write a shell script wrapper for it.
Thank you for your prompt replies! I want it to Tidy my Java code and am interested in trying to use a Java parser. Do you have any suggestions for a Java Parser? I found this one: http://javaparser.github.io/javaparser/
I assume I would need a shell script wrapper for the Java Parser, as well, correct?
Do you have any resources on how to build a shell script wrapper for TextWrangler?
You can just call java from the command line, so using the XML example, replace the commands with the call to your class. If you use any third party libraries you’ll need them on the class path too which you can add with the -cp flag.
Thankyou this worked perfectly in texwrangler version 5.0.2 (3786).
Thanks a lot.
Awesome! Thank you!
Perfect! Thanks, mate – exactly what I wanted! 😀
Thanks for this script!
My only was when JSON isn’t valid, the original file is truncated (because nothing is printed) and the exception is showed as a new file in TextWrangler (which is OK). I modified it to preserve the original file:
“`
#!/usr/bin/python
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),
“`
Very cool thx !
Works great, thank you!
Now I need to do the opposite: convert a nicely formatted JSON object into a single line…
This is what I have been looking for mean while I have used, https://jsonformatter.org/xml-pretty-print for XML and https://jsonformatter.org/json-pretty-print for JSON
Thank you for providing this. I just added it to BBEdit v13 and it works a treat for cleaning up even quite larg JSON files.