Created
February 1, 2017 21:51
-
-
Save andymeneely/17fee4895ab20ec1f8c63023cabad830 to your computer and use it in GitHub Desktop.
Random json parsing script to convert to csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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