I have a hash response from a JAVA service which looks like this :
jsonResponse = {:json=>{"reply"=>[{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "score"=>"1"}, {"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "score"=>"2"},{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "score"=>"3"}]}, :status=>200}
I have to add one key value pair at a specific position to each of these reply array objects, so that the response transforms to something like this , to make it simpler for now, lets try adding a samke key value pair at a particular position:
jsonResponse = {:json=>{"reply"=>[{"person"=>"abc", "roll_no"=>"1234","location"=>"loc1", "new_value => "new_result", "score"=>"1"}, {"person"=>"def", "roll_no"=>"1235", "location"=>"loc2","new_value => "new_result", "score"=>"2"},{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "new_value => "new_result", "score"=>"3"}]}, :status=>200}
This is what I have tried ,I run .each through jsonResponse :
jsonResponse[:json]['reply'].each do |object|
objectArray = object.to_a
insert_at = objectArray.index(objectArray.assoc('score'))
object = Hash[objectArray.insert(insert_at, ['new_value','new_result'])]
print("\n\nTest\n\n")
print object
end
print("\n\nFinal Response\n\n")
print jsonResponse
The object which i am printing has the desired response but it does not get updated in the jsonResponse
This is the output of the above code snippet:
Test
{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "new_value"=>"new_result", "score"=>"1"}
Test
{"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "new_value"=>"new_result", "score"=>"2"}
Test
{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "new_value"=>"new_result", "score"=>"3"}
Final Response
{:json=>{"reply"=>[{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "score"=>"1"}, {"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "score"=>"2"}, {"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "score"=>"3"}]}, :status=>200}
Aucun commentaire:
Enregistrer un commentaire