JSON parsing with ruby one-liner ?
Parsing JSON is one of the most common tasks that any web developer does in his daily activities.
Parsing JSON
with ruby
with is fun and the fun multiples n.times
when the code is a one-liner
Indeed some gems are needed along with ruby and rubygems, you shall need :
gem install rest-client
With all the above gems, begins the fun! Say you have a simple json that gives the IP of your machine unlike http://h3manth.com/ip.php which was specifically designed for jsonp!
So say the json string is like {ip:192.71.223.65}
Using the simple ruby one-liner below, this is what happens with that json:
require 'rubygems' require 'json' require 'rest-client' json_ip_url = "http://ifconfig.me/all.json" # The killer one line ruby json parsing! ip_details = JSON.parse(RestClient.get(json_ip_url))
The json from the URL looks like :
{ "connection": "keep-alive", "ip_addr": "192.71.223.65", "lang": "en-US,en;q=0.8", "remote_host": "NSG-Static-066.223.71.182.airtel.in", "user_agent": "Mozilla/5.0", "charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3", "port": "4532", "via": "", "forwarded": "", "mime":"text/html,application/xhtml+xml,application/xml;q=0.9", "keep_alive": "", "encoding": "gzip,deflate,sdch" }
And the killer ruby one line JSON parser Crack::JSON.parse(RestClient.get(json_ip_url))
returns a wonderful hash like :
{ "connection" = > "", "ip_addr" = > "192.71.223.65", "lang" = > "", "remote_host" = > "NSG-Static-066.223.71.182.airtel.in", "user_agent" = > "Ruby", "charset" = > "", "port" = > "24436", "via" = > "", "forwarded" = > "", "mime" = > "* / *; q=0.5, application/xml", "keep_alive" = > "", "encoding" = > "gzip, deflate" }
With that, one can just use ip_details["json_key"]
to get the required details!
Let's try something more, one liners are always fun and when it's ruby and the web, it's more fun!
Let's get the top three images from Google image search!
#!/usr/bin/env ruby require 'rubygems' require 'json' require 'rest-client' require 'uri' def get_imgs(term) url='https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q='+URI.escape(term) imgs = JSON.parse(RestClient.get(url)) for img in imgs["responseData"]["results"] puts img["url"] end end =being get_imgs("yoda") would print http://images.wikia.com/starwars/images/e/e0/Yoda_SWSB.jpg http://upload.wikimedia.org/wikipedia/en/thumb/9/96/CGIYoda.jpg/200px-CGIYoda.jpg http://i.dailymail.co.uk/i/pix/2010/10/06/article-1318093-0B803914000005DC-954_306x423.jpg http://altjapan.typepad.com/.a/6a00d8341bfd2253ef00e55426d7bb8834-320wi =end
So far this has been my best way of parsing JSON with ruby, what's yours?
Edit : Define one-liner? :D It must have been JSON parsing in a line, right?
Recent blog posts
- watir-webdriver web inspector
- gem list to gemfile
- Packing ruby2.0 on debian.
- Made it into The Guinness Book!
- to_h in ruby 2.0
- Filter elements by pattern jQuery.
- Better HTML password fields for mobile ?
- Grayscale image when user offline
- nth-child CSS pseudo-class Christmas colors
- EventEmitter in nodejs