Created
December 18, 2013 04:13
-
-
Save qpark99/8017175 to your computer and use it in GitHub Desktop.
CSV to JSON ruby script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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