Uri Agassi's code is perfect, but I thought to explain what went wrong in the OP code.
The Hash[]
expects an array of key, value
pairs (either as separate arguments or an array of arrays):
Hash[:a, 1, :b, 2] # => {:a=>1, :b=>2}
Hash[[[:a,1], [:b,2]]] # => {:a=>1, :b=>2}
But the original JSON contained Array of Hashes which gets parsed into corresponding Ruby objects as in the simplified case:
[{:a => 1}, {:b => 2}]
When you use the Ruby splat operator *
on the above array:
Hash[ *[{:a => 1}, {:b => 2}] ]
You efectively provide separate hashes as a key-value pair to the Hash constructor:
Hash[ {:a => 1}, {:b => 2} ] # => {{:a=>1} => {:b=>2}}
So, essentially, you got what you asked for: you turned a array of hashes into a hash of hashes and then you added that to a blank array and topped up with another hash.
Correct solution
Just to make the original code work with minimal changes (but still take a look at Uri's solution):
tempHash = {"Group_Name" => @GroupName, "Group_Logo_Code" => @GroupLogoCode }
json = File.read('public/group.json')
secondJsonArray = JSON.parse(json)
secondJsonArray << tempHash
File.open("public/group.json","w") do |f|
f.puts JSON.pretty_generate(secondJsonArray)
end