JSON with Ruby


Before you can start encoding and uninstalling JSON using Ruby, you need to install any JSON modules available on Ruby. You may need to install a Ruby gem, but if you are using the latest version of Ruby, you will need to have a gem already installed on your machine; otherwise, let's follow one step further, assuming you already have the installed gem -

$ gem insert json

Separate JSON using Ruby

The following example shows that the first two buttons hold the string values , and the last three keys hold the string layout. Let's keep the following content in a file called input.json.

{ "President": "Alan Isaac", "General Manager": "David Richardson", "India": [ "Sachin Tendulkar", "Virender Sehwag", Gautam Gambhir ], "Sri Lanka": [ "Lasith Malinga", "Angelo Mathews", "Kumar Sangakkara" ], "England": [ "Alastair Cook", "Jonathan Trott", "Kevin Pietersen" ] }

As given below is a Ruby program that will be used to separate the above-mentioned JSON text

#! / usr / bin / ruby requires 'rubygems' dinga 'json' search for 'pp' json = File.read ('input.json') obj = JSON.parse (json) kk obj

When operational, it will produce the following results -

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