Command line script to Pretty Print a JSON URL
August 5, 2010 3 Comments
Don’t you hate it when you curl an api for testing and get something ugly like:
curl http://search.twitter.com/search.json?q=ruby q=ruby","next_page":"?page=2&max_id=20407667370&q=ruby","results_per_page":15,"page":1,"completed_in":0.018639,"query":"ruby"}
Here’s a quick one line bash script to pretty print a json url using curl and ruby:
#!/bin/bash
curl $* | ruby -e "require 'rubygems';require 'json'; jj JSON.parse(STDIN.gets)"
Save it as ppcurl, set the permissions (chmod a+x ppcurl) and run it:
ppcurl http://search.twitter.com/search.json?q=ruby
{
"created_at": "Thu, 05 Aug 2010 18:25:41 +0000",
"profile_image_url": "http://a3.twimg.com/profile_images/326153315/twitterProfilePhoto_normal.jpg",
"from_user": "der_kronn",
"text": "interesting gem: "Zucker" http://bit.ly/9kiJ3I - cool to see, how flexible ruby is /cc @rbJL",
"to_user_id": null,
"metadata": {
"result_type": "recent"
},
"id": 20407489891,
"geo": null,
"from_user_id": 20887268,
"iso_language_code": "en",
"source": "<a href="http://termtter.org/" rel="nofollow">Termtter</a>"
}
],
"since_id": 0,
"refresh_url": "?since_id=20407667370&q=ruby",
"next_page": "?page=2&max_id=20407667370&q=ruby",
"page": 1,
"results_per_page": 15,
"completed_in": 0.0169860000000001,
"query": "ruby"
}
Much nicer!
Now what if you only want to see subsets of the JSON output, or write simple shell scripts to test individual JSON values? Check out Jazor and send me your feedback. =)https://github.com/mconigliaro…
Using $* instead of $1 allows you to pass arguments such as “-X POST” to ppcurl.
#!/bin/bash
curl $* | ruby -e “require ‘rubygems’;require ‘json’; jj JSON.parse(STDIN.gets)”
Thanks for the snippet!
Thanks! I’ll edit the post.