Skip to content

Instantly share code, notes, and snippets.

@andymeneely
Created February 1, 2017 21:51
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 andymeneely/17fee4895ab20ec1f8c63023cabad830 to your computer and use it in GitHub Desktop.
Save andymeneely/17fee4895ab20ec1f8c63023cabad830 to your computer and use it in GitHub Desktop.
Random json parsing script to convert to csv
require 'json'
require 'csv'
puts "Parsing..."
data = JSON.parse(File.read('LocationHistory.json'))
puts "Saving..."
CSV.open("locations.csv", "w+") do |csv|
headers = %w( timestampMs latitudeE7 longitudeE7 accuracy )
headers += (0..10).to_a.map { |i| "activity_#{i}" }
csv << headers
data['locations'].each.with_index do |location|
row = [
location['timestampMs'],
location['latitudeE7'],
location['longitudeE7'],
location['accuracy'],
]
unless location['activitys'].nil?
row << location['activitys'][0]['timestampMs']
unless location['activitys'][0]['activities'].nil?
location['activitys'][0]['activities'].each do |activity|
row << activity['type']
row << activity['confidence']
end
end
end
csv << row
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment