I have a hash response object 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}
Q2. Also as you can see from the code snippet the insert_at logic works in a way that it adds before the position we specify, for eg it adds before score key , is there a logic which i can write which adds to the position after the specified key and not before?
Appreciate the efforts by everyone
We are given three objects.
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
}
key_value_pair_to_add = { 'new_value'=>'new_result' }
key_to_precede = 'location'
We then modify jsonResponse
as follows.
keys_to_shift = jsonResponse[:json]['reply'][0].keys.
drop_while { |k| k != key_to_precede }
#=> ["location", "score"]
jsonResponse[:json]['reply'].each do |h|
h.update('new_value'=>'new_result')
keys_to_shift.each { |k| h.update(k=>h.delete(k)) }
end
jsonResponse
#=> {
# :json=>{
# "reply"=>[
# {"person"=>"abc", "roll_no"=>"1234", "new_value"=>"new_result",
# "location"=>"loc1", "score"=>"1"},
# {"person"=>"def", "roll_no"=>"1235", "new_value"=>"new_result",
# "location"=>"loc2", "score"=>"2"},
# {"person"=>"fgh", "roll_no"=>"1236", "new_value"=>"new_result",
# "location"=>"loc3", "score"=>"3"}
# ]
# },
# :status=>200
# }
See Hash#update (aka merge!
) and Hash#delete .
h.delete('location')
removes the key-value pair 'location'=>'locX'
from h
and returns locX
, after which
h.update('location'=>'locX')
returns that key-value pair to the end of the hash. This is repeated for each key in keys_to_shift
.
Give a simple example:
# define order
order = [:first, :second, :third]
# the hash you want to order by
json = { :third=>"3", :second=>2, :first=>1 }
result = json.sort_by { |k, _| order.index(k) }.to_h
then result will be {:first=>1, :second=>2, :third=>"3"}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.