Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Convert Excel of Arbitrary Tasks to JSON
#!/usr/bin/env ruby
# Reads an Excel and writes as JSON
# - Assumes header row containing field names, first sheet is read
# - Keeps null values
# - accept filename and read based on extension (xlsx, xls, csv).
# - output actual JSON.
# - clean up.
require 'rubygems'
require 'roo'
require 'json'
xlsx ="doc/Tasks.xlsx")
xlsx.default_sheet = xlsx.sheets.first
field_names = xlsx.row(xlsx.header_line)
arr = []
(xlsx.first_row + 1).upto(xlsx.last_row) do |line|
obj = {}
0.upto(field_names.count - 1) do |i|
obj[field_names[i]] = xlsx.row(line)[i]
arr << obj
puts JSON.pretty_generate arr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment