Created
February 17, 2020 07:00
-
-
Save yazinsai/b2552a06a4aec6c2b1eb0b55fc637734 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=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