-2

The following response is returned by an external API and is assigned to ret_val:

{:val1=>"add", :val2=>"delete", :val3=>"update"}
{:val1=>"add_name", :val2=>"delete_name", :val3=>"update_name"}
{:val1=>"add_city", :val2=>"delete_city", :val3=>"city"}

I want to convert ret_val into an array so that the values assigned to val1, val2, and val3 can be extracted by giving val1, val2, and val3.So basically I would like to see key and value pair of all the comma separated pairs . Expected output :

val1 : add
val2 : delete
val3:update

till the last row returned by the API. Please suggest. Note: API will always return response in above format only with duplicate key.

6
  • 1
    What exactly is the expected result of one of your example Hash?
    – Yu Hao
    Commented Apr 26, 2015 at 8:42
  • Can you add example for one of the Hash to understand it better? Commented Apr 26, 2015 at 8:48
  • what you intend to do is not clear. Commented Apr 26, 2015 at 9:46
  • why so much down vote for this??? it is clear that API is returning this response and same is captured in one string , now I need to parse the string so that corresponding value can be set. Commented Apr 26, 2015 at 11:18
  • 1
    This question is already solved yesterday by user2907032. Why do u want to put on hold . Commented Apr 27, 2015 at 15:18

3 Answers 3

1

below code snippet will give you desire result:

#ret_val=calling API
ret_val.each { |x|  
x.each { |val| 
puts "#{val[0]} :#{val[1]}"
}
}
2
  • Thanks , Now I am able to get the desire results. Commented Apr 26, 2015 at 13:46
  • By seeing API response anyone can easily make out that it shouldn't be hash object as hash object doesn't allow duplicate key. Commented Apr 26, 2015 at 13:49
1

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"]
9
  • how it can be hash, a hash can't have duplicate keys. My code is giving error. Commented Apr 26, 2015 at 11:24
  • Now I'm confused ;) Isn't a hash like in the example what you get from the API? Or does the API return an array whose member items are hashes? Commented Apr 26, 2015 at 13:30
  • Yes, because duplicate can't be allowed in hash and to allow the same API is behaving like this. Commented Apr 26, 2015 at 13:45
  • "Yes" to which of the two possibilities? ;) Maybe it would help if you edited your question with a concrete example of what you get from the API and a concrete exampe of the structure you would like to transform it into. Commented Apr 26, 2015 at 13:48
  • currently I have posted only 3 rows but in actual response it were 30 rows. I can't reveal here exact data for response so just shared dummy values only. After getting this values , I need to set this in JSON payload to update the operation . Commented Apr 26, 2015 at 14:01
0

What the API is returning is a hash. You can access a hash value by its key.

ret_val = func()

ret_val is a hash object. To access its values

a = Array.new()
ret_val.each { |key,value| a << value } 
2
  • It doesn't work and I am getting nothing with this. Commented Apr 26, 2015 at 11:31
  • API is returning Array so ret_val is a Array object as hash object can't have similar keys. Commented Apr 26, 2015 at 12:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.