Q:

Erase Blank Json Loop

I have a rails backend that recieves JSON that (when parsed) looks like:
[
   {"kind"=>"Magazine", "price"=>["$20.99"]},
   {"kind"=>"Book", "price"=>"", "title"=>""}
]

Basically what I want to do is for each kind of product (e.g. Magazine or book), if all other attributes except for the kind key are blank, then don't save that array key/value. So in my example, Magazine would stay in the array, but the Book kind would be deleted (because both attributes price and title are blank.
I know I could loop through with something like (list is the parsed JSON before):
list.each do |l|
    if l["kind"] == "Magazine"
       if l["price"].blank?
          # THEN DELETE THIS ITERATION
       end
    end
end 

but this seems very repetitive and not clean. How do I do this better?

A:

The idiomatic way to do this would involve using Array#reject! to remove the unwanted lines.   You can also extract just the values, remove the blank ones, and count the remaining values... making sure that 'kind' is one of them...
list.reject! {|item| item.values.reject(&:blank?).size < 2 && item['kind'].present?}

Notice the difference between reject and reject! ... one returns a new hash while the ! method modifies it in place.

