Skip to content

Instantly share code, notes, and snippets.

@eskfung
Created November 20, 2014 20:22
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 eskfung/f0d6bac6e196bd77228c to your computer and use it in GitHub Desktop.
Save eskfung/f0d6bac6e196bd77228c to your computer and use it in GitHub Desktop.
KM People JSON to CSV
require 'csv'
data = {
# simplifying returned results
"total" => nil,
"links" => [],
"metadata" => {},
"columns"=> [
{
"display_name"=>"KMID",
"type"=>"internal",
"return_option"=>"internal",
"display_return_option"=>"internal"
},
{
"display_name"=>"Person",
"type"=>"internal",
"return_option"=>"internal",
"display_return_option"=>"internal"
},
{
"type"=>"event",
"display_name"=>"Visited Site",
"return_option"=>"total_times_ever",
"display_return_option"=>"Total Times Ever"
}
],
"rows"=> [
[12345, "bob@kissmetrics.com", 22]
]
}
CSV.generate do |writer|
writer << data.fetch('columns').map {|column| column.fetch('display_name', '')}
data.fetch('rows').each do |row|
writer << row
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment