I am collecting a response from a web service in a Rails' app with
response = #api.sql_data This causes response to equal the following.
response #=> { "success"=>true, "totalcount"=>10, "rows"=>[ # { "ID"=>"0001", "CODE"=>"0000001", "CODE_1"=>"Alpha", # "NAME"=>"Alpha", "Balance"=>"0" }, # { "ID"=>"0002", "CODE"=>"0000002", "CODE_1"=>"Beta", # "NAME"=>"Beta", "Balance"=>"0" }, # { "ID"=>"0003", "CODE"=>"0000003", "CODE_1"=>"Charlie", # "NAME"=>"Charlie", "Balance"=>"0"}, # ... # ] # } I have created the following method:
def format_response(response) response['rows'].map do |row| { id: row[0], code: row[1], code1: row[2], balance: row[4] } end.uniq { |x| x[:code1] } end When I execute this method for the above value of response, I obtain the following.
format_response(response) #=> [{:id=>nil, :code=>nil, :code1=>nil, :balance=>nil}] This is incorrect. I would like this expression to return the following.
{"ID"=>"0001", "CODE"=>"0000001", "CODE_1"=>"Alpha", "NAME"=>"Alpha", "Balance"=>"0"} What is my mistake?