Skip to content

Instantly share code, notes, and snippets.

@mariusbutuc
Forked from enriclluelles/csv_to_json.rb
Last active August 29, 2015 14:22
Show Gist options
  • Save mariusbutuc/73c4a11ccc627c537095 to your computer and use it in GitHub Desktop.
Save mariusbutuc/73c4a11ccc627c537095 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'csv'
require 'json'
if ARGV.size != 2
puts 'Usage: csv_to_json input_file.csv output_file.json'
puts 'This script uses the first line of the csv file as the keys for the JSON properties of the objects'
exit(1)
end
extracted_data = CSV.table(ARGV[0])
transformed_data = extracted_data.map { |row| row.to_hash }
File.open(ARGV[1], 'w') do |file|
file.puts JSON.pretty_generate(transformed_data)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment