JSON - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JSON Ruby

JSON Ruby

shape Description

JSON is always user-friendly, the developer can easily parsing JSON with Ruby language, before going to see how to parse JSON with Ruby it's better to know about Ruby language. Ruby is an object-oriented programming language like java, Smalltalk. The syntax of Ruby is very simple compared to Smalltalk. All the files of Ruby will be saved with .rb extension. Ruby has been picking up notoriety, and a structure called Ruby on Rails has expanded its use of web programming. The Ruby dialect is completely object oriented, in that everything is an object. For example, even the most essential information sorts like numbers have techniques and case factors. This gives a more prominent capacity to utilize strategy chaining, where numerous lines of code can be merged into one. Following is an example. [html] #!/usr/bin/ruby -w puts "Welcome to splessons...!"; [/html] Now save the above code with test.rb. Now run the above code as follows. [html]$ ruby test.rb[/html] Now the result will be as follows. [html]Welcome to splessons...![/html]

Feature Of Ruby

shape Description

The following are the various features of Ruby. Following is the simple example for Ruby. [html] #!/usr/bin/ruby class Sample def hello puts "Hello Ruby!" end end # Now using above class to create objects object = Sample. new object.hello [/html] The following is the code description. Now compile the code result will be as follows. [html]Hello Ruby![/html]

Parsing JSON With Ruby

shape Description

As earlier discussed JSON easily can integrate with other languages such as JAVA, PHP, Ruby. It's anything but difficult to bounce into parsing and producing JSON in Ruby with the JSON gem. It gives an API to parsing JSON from text and creating JSON content from discretionary Ruby objects. It's effectively the most utilized JSON library as a part of Ruby. Following is the source code for the JSON, keep this code in input.json file. [html] { "President": "Alan Isaac", "CEO": "David Richardson", "India": [ "Sachin Tendulkar", "Virender Sehwag", "Gautam Gambhir" ], "Srilanka": [ "Lasith Malinga", "Angelo Mathews", "Kumar Sangakkara" ], "England": [ "Alastair Cook", "Jonathan Trott", "Kevin Pietersen" ] } [/html] In the above example, JSON objects and array values have placed. The following is the source code for ruby that includes input.json file also and save this file with main.rb. [html] #!/usr/bin/ruby require 'rubygems' require 'json' require 'pp' json = File.read('input.json') obj = JSON.parse(json) pp obj [/html] Where RubyGems will select the latest installed version of the gems that follow. If no such software is found, an exception is generated. Now compile the code from the main.rb file by using Ruby tool as follows. First, enter the filename with correct syntax as follows. When compiling the code by using above syntax then the result will be as follows. Following is the result displayed in the above Ruby tool.
{ "President"=>"Alan Isaac", "CEO"=>"David Richardson", "India"=> ["Sachin Tendulkar", "Virender Sehwag", "Gautam Gambhir"], "Srilanka"=> ["Lasith Malinga ", "Angelo Mathews", "Kumar Sangakkara"], "England"=> ["Alastair Cook", "Jonathan Trott", "Kevin Pietersen"] }

Summary

shape Key Points

  • Ruby is server side scripting language.
  • In Ruby local variables will start with lower case letter or with _.
  • Ruby is very friendly and open source and object oriented language.
  • Ruby has pre-composed structures and libraries like Ruby on Rails and Chef.