Skip to content

Instantly share code, notes, and snippets.

@benjaminjackson
Created July 26, 2013 16:58
Show Gist options
  • Save benjaminjackson/6090463 to your computer and use it in GitHub Desktop.
Save benjaminjackson/6090463 to your computer and use it in GitHub Desktop.
No more copying and pasting Word docs into JSON files.
#! /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