Skip to main content

Pretty printing JSON and XML in the shell

Last year I read Craig Hockenberry’s magnus opus – a huge collection of information about the OS X Terminal and the Bash shell. It’s worth reading in its entirety, even if you don’t use a Mac. My favourite tips were opt-clicking to jump around in Terminal (which makes Vim far more usable for me), and using the lsof command when a disk won’t eject. Both super useful.

At the same time, I read Dr. Drang’s suggestion that we write up our own tips. I wrote this post at the time, and then it sat in drafts for over a year. Oops. Anyway, here are a few tips from me.

I work a lot with website APIs, which typically return responses that are formatted in XML or JSON. To save bandwidth, these responses are minified (stripped of unnecessary whitespace and newlines). That’s fine for a program, but it’s quite painful if I’m reading it.

Luckily, OS X includes a few tools for pretty printing JSON and XML. This makes the responses substantially easier to read for a human.