Updated answer: If you just want to reproduce the very same structure in JSON, you can just use Ruby's JSON module
require 'json'
ret_val = [
{ :val1 => "add", :val2 => "delete", :val3 => "update" },
{ :val1 => "add_name", :val2 => "delete_name", :val3 => "update_name" },
{ :val1 => "add_city", :val2 => "delete_city", :val3 => "update_city" }
]
puts ret_val.to_json
# => [{"val1":"add","val2":"delete","val3":"update"},{"val1":"add_name","val2":"delete_name","val3":"update_name"},{"val1":"add_city","val2":"delete_city","val3":"update_city"}]
If you need to rework the structure, you need to iterate over the hashes in the response array, for example with Array#each
. You can associate a block with the function and execute the block for each entry in the array. To illustrate, let's look at my_array = ["one", "two", "three"]
my_array = ["one", "two", "three"]
my_array.each do |e|
# e is the current entry
puts "The current entry is '#{e}'"
end
The output would be
The current entry is 'one'
The current entry is 'two'
The current entry is 'three'
In the same way you can iterate over the hashes in the array you've got from the API and handle each hash like you want to
ret_val = ... # same as above
ret_val.each do |hash|
# do whatever you need to do with 'hash'
end
What you're looking for is probably Hash#values
my_hash = {:val1=>"add", :val2=>"delete", :val3=>"update"}
my_hash.values # => ["add", "delete", "update"]