Skip to content

Instantly share code, notes, and snippets.

@qpark99
Created December 18, 2013 04:13
Show Gist options
  • Save qpark99/8017175 to your computer and use it in GitHub Desktop.
Save qpark99/8017175 to your computer and use it in GitHub Desktop.
CSV to JSON ruby script
require 'csv'
require 'json'
require 'open-uri'
if ARGV[0].nil?
raise "wrong number of arguments (0 for 1..2)"
end
file = open(ARGV[0])
file.set_encoding("UTF-8")
header = nil
rows = []
CSV.parse(file.read).each do |line|
if header.nil?
header = line.map(&:strip)
next
end
rows << Hash[header.zip(line.map(&:strip))]
end
File.open(ARGV[1] || 'to-json.js', 'w') {|f| f << JSON.pretty_generate(rows) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment