Sha256: 32eae59b3d621535cdc8b9ffbe71a4cb4c8ee3fcb38a6eb88864102cc1eae0f6

Contents?: true

Size: 836 Bytes

Versions: 1

Compression:

Stored size: 836 Bytes

Contents

require 'rubygems'
require 'csv'
require 'json'
require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'csv2json-version.rb')

module JSON2CSV

    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

    module_function :parse
    
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csv2json-0.3.0 lib/json2csv.rb