This post is just a short tidbit, a tip how to pretty print JSON when using Curl.
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:
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
There are several posts about this very same issue:
- A Stackoverflow question/answer that uses the same approach I do
- Same approach taken by Frederic Campus but with JSON parsing
- Alternative ways to pretty print JSON by Alan Skorkin
Now, if you prefer an online tool, there are several of those available too. Here's two: