Ruby: How can I convert an array of data to hash and to json format?

ruby

Solution

array.map { |o| Hash[o.each_pair.to_a] }.to_json

Problem

I am very new to Ruby array and hash manipulation. How can I do this simple transformation? ``` array = [#<struct id=1, car='red'>, #<struct id=2, car='yellow'>, #<struct id=3, car='green'>] ``` desired output in json: ``` [{id : 1, car : 'red'} , {id:2, car :'yellow'} ,{id:3 , car: "green"}] ``` Does anyone have any hints?

Original source