Skip to content

Instantly share code, notes, and snippets.

@branch14
Created November 13, 2013 10:29
Show Gist options
  • Save branch14/7446901 to your computer and use it in GitHub Desktop.
Save branch14/7446901 to your computer and use it in GitHub Desktop.
quickly convert json (array of equally structured objects) into csv
#!/usr/bin/env ruby
require 'json'
require 'csv'
CSV do |csv|
data = JSON.parse(ARGF.read)
csv << data.first.keys.sort
data.each do |line|
csv << line.to_a.sort_by { |a| a.first }.map { |a| a.last }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment