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"]
}