Created
July 26, 2013 16:58
-
-
Save benjaminjackson/6090463 to your computer and use it in GitHub Desktop.
No more copying and pasting Word docs into JSON files.
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/ruby | |
require 'iconv' | |
require 'roo' | |
require 'csv' | |
require 'json' | |
begin | |
ARGV.each do |file| | |
file_basename = File.basename(file, ".xlsx") | |
puts "Opening #{file}" | |
xls = Roo::Excelx.new(file) | |
csv = CSV.parse(xls.to_csv) | |
properties = csv.shift | |
json_array = csv.map do |record| | |
dict = {} | |
properties.each_with_index do |key, index| | |
if record[index] =~ /^[YN]$/i | |
dict[key] = record[index].downcase == "y" | |
elsif record[index] =~ /^[0-9.]+$/i | |
dict[key] = record[index].to_f | |
else | |
dict[key] = record[index] | |
end | |
end | |
dict | |
end | |
File.open("#{File.dirname(file)}/#{file_basename}.json", "w") do |f| | |
f.write(json_array.to_json) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment