Created
July 8, 2012 15:51
-
-
Save mdobson/3071422 to your computer and use it in GitHub Desktop.
Convert CSV to JSON with simple command line 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
#!/usr/bin/env ruby | |
require 'json' | |
file = File.new(ARGV[0], "r") | |
#get format | |
puts "Getting format" | |
format = file.gets | |
data_points = format.split(',') | |
puts "Data Point Labels #{data_points}" | |
#read data | |
puts "Reading data" | |
while(line = file.gets) | |
data = line.split(',') | |
index = 0 | |
data_doc = {} | |
data_points.each do |label| | |
data_doc[label.strip] = data[index].strip | |
index = index + 1 | |
end | |
puts "Document: #{data_doc.to_json}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment