Skip to content

Instantly share code, notes, and snippets.

@armstrongnate
Created December 19, 2014 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armstrongnate/6db0b2d81d6803063235 to your computer and use it in GitHub Desktop.
Save armstrongnate/6db0b2d81d6803063235 to your computer and use it in GitHub Desktop.
convert a csv file to json
require 'csv'
require 'json'
date = 5
file_name = "weather-2014-12-#{date}"
body = File.open("/path/to/file/#{file_name}.csv", 'rb').read
csv = CSV.new(body, :headers => true, :header_converters => :symbol, :converters => :all)
weather = csv.to_a.map {|row| row.to_hash }
File.open("/path/to/file/#{file_name}.json", 'w') { |f| f.write(weather.to_json) }
# replace null with "" manually
# run putil command
# plutil -convert xml1 weather-2014-12-[DATE].json -o weather-2014-12-[DATE].plist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment