Module: JSON2CSV
- Defined in:
- lib/json2csv.rb
Class Method Summary collapse
Class Method Details
.parse(input, output, headers = nil, csvOptions = nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/json2csv.rb', line 8 def parse(input, output, headers=nil, csvOptions=nil) json = JSON.parse(input) unless headers headers = json[0].keys end outputCSV = CSV.generate(csvOptions) do |csv| csv << headers json.each do |obj| sortedValues = Array.new(headers.length) obj.each do |key, val| headers.each_index { |i| sortedValues[i] = val if headers[i] == key } end csv << sortedValues end end output << outputCSV end |