I am using ruby version 2.0.0, I have a demo.json file which looks like this:
{ "demo":
{
"rama" : { "Name": "demo" },
"krishna" : { "Name": "hare","place": "bharat", "hawa": { "maina": "tota"} }
}
}
Now I try to manipulate json file by this way:
require 'json'
options = {}
options[:demo] = "kailash"
File.open("demo.json","w") do |f|
f.write(JSON.pretty_generate(options))
end
I want to replace some values and to add some new key-value pairs in the existing JSON file and don't wants to completely replace the entire JSON file. Is there any way to do this?
JSON::parsemethod, to get the hash object first. Then iterate though the hash, and update the value whatever you want. When done, useHash#to_jsonmethod to make it aJSONobject and then write it back to the file..jsonfile ?