Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a key from every element in a JSON Array - Ruby

Tags:

json

arrays

ruby

So, I have a JSON array through which I am looping, and adding three keys and their values, I also want to remove/delete a key(subject_id) from every element in the array.

How to go about doing this?

s=JSON.parse(subjects.to_json)
s.each do |j|
  j["total_classes"] = total_classes[i]
  j["attended_classes"] = attended_classes[i]
  j["subject_name"] = subject_name[i]
  j["subject_id"] #remove this key, how?
  i=i+1
end

render json: { attendances: s }
like image 655
srijanshukla Avatar asked Jan 03 '17 18:01

srijanshukla


Video Answer


1 Answers

Try this one

j.delete("subject_id")
like image 163
Ursus Avatar answered Nov 15 '22 08:11

Ursus