Skip to content

Instantly share code, notes, and snippets.

@pritibiyani
Created January 13, 2015 09:45
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 pritibiyani/631bfec84b892d76aca6 to your computer and use it in GitHub Desktop.
Save pritibiyani/631bfec84b892d76aca6 to your computer and use it in GitHub Desktop.
Usage: ./json_to_csv.rb {jsonFile}
#! /usr/bin/env ruby
# encoding: UTF-8
require 'csv'
require 'json'
f = File.open("./#{ARGV[0]}", "r")
csv_string = CSV.generate do |csv|
f.each_line do |json|
begin
hash = JSON.parse(json)
csv << hash.values
rescue Exception => e
puts e
end
end
end
File.open("#{ARGV[0]}.csv", "w") { |f| f.write(csv_string) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment