CSV to JSON Convert Ruby

Here. Easy way to convert your CSV (with headers) to a representative json hash. It just creates a file with .json tacked onto the end.

require 'json'
require 'csv'


File.open("#{file_name}.json", "w") { |f| f.write(CSV.open(file_name, headers: true, header_converters: :symbol, converters: :all).to_a.map(&:to_hash).to_json) }
About the Author: JeffAncel
A passionate web developer in St. Louis. Living on the edge with Rails, HTML5, CSS3 and any interesting libraries around. I love to spend time with the family, cook, develop web applications and play with our dragons. When I have nothing else to do, I socialize via twitter (@jancel) and facebook.

Leave a reply

© 2012 - Jeff Ancel
Wordpress Themes
Scroll to Top