Skip to content

Instantly share code, notes, and snippets.

@yazinsai
Created February 17, 2020 07:00
Show Gist options
  • Save yazinsai/b2552a06a4aec6c2b1eb0b55fc637734 to your computer and use it in GitHub Desktop.
Save yazinsai/b2552a06a4aec6c2b1eb0b55fc637734 to your computer and use it in GitHub Desktop.
=begin
Converts:
# people.csv
Name, Age, Height
Ahmed, 12, 103
Mohamed, 19, 176
.. into:
# people.json
[
{
"name": "Ahmed",
"age": 12,
"height": 103
},
{
"name": "Mohamed",
"age": 19,
"height": 176
},
]
=end
def csv_to_json(file_in)
csv = CSV.read(file_in, headers: true)
json = []
csv.each {|line| json << line.to_h }
# write output
file_out = file_in.gsub(/\.csv/, ".json")
File.open(file_out, "w") {|f| f.write json.to_json }
puts "✅ All done"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment