Command line script to Pretty Print a JSON URL

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!

About tommychheng
I write a tech blog at http://tommy.chheng.com

3 Responses to Command line script to Pretty Print a JSON URL

  1. 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…

  2. Andrew Ashbacher says:

    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!

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.