This post is just a short tidbit, a tip how to pretty print JSON when using Curl.

The Solution

As an example, I'll request my latest post as JSON and pretty print it.

curl -H "Accept: application/json" http://hkionline.net/posts/latest | python -m json.tool

In the example, we first make a HTTP GET request with Curl. Additionally, I set Curl only to accept application/json. This way my site will produce json, not html which is the default. Then I pipe the result of the request to a python's json library. This approach should work as long as you have Curl and Python installed (as is the case with OS X and most *nix systems).

How about a shell script

Create the script file:

vim ppjson.sh

Add the curl-command to the script, use $1 to capture the first command line argument:

#!/bin/bash
curl -H "Accept: application/json" $1 | python -m json.tool

Add execution permissions for the script:

chmod u+x ppjson.sh

Done.

Common Issue

There are several posts about this very same issue:

Now, if you prefer an online tool, there are several of those available too. Here's two: