Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Converts a Google Location JSON file into a CSV with Timestamp, Latitude, and Longitude.
import re
import json
import csv
JSON_data="C:/Users/JSON FILE PATH.txt"
CSV_file="C:/Users/CSV FILE PATH.csv"
with open(JSON_data) as f:
data = json.load(f)
csv_f=open(CSV_file, 'w')
for x in range(len(data["locations"])):
regex_Time=re.findall(".timestampMs.: .\d\d\d\d\d\d\d\d\d\d\d\d\d.", str(data["locations"][x]))
regex_Lat=re.findall(".latitude...: -?\d\d\d\d\d\d\d\d\d", str(data["locations"][x]))
regex_Long=re.findall(".longitude...: -?\d\d\d\d\d\d\d\d\d", str(data["locations"][x]))
csv_f.write(regex_Time[0] + "," + regex_Lat[0] + "," + regex_Long[0]+'\n')
csv_f.close
print('Done')
@troldmand
Copy link

troldmand commented Sep 17, 2020

It's this one, that doens't work:
regex_Long[0][15:]

@troldmand
Copy link

troldmand commented Sep 17, 2020

Ah! We found (my girlfriend and I), that there's a typo in l. 15: remove the dash before the /d's

regex_Long=re.findall(".longitude...: -\d\d\d\d\d\d\d\d\d", str(data["locations"][x]))

should be
regex_Long=re.findall(".longitude...: \d\d\d\d\d\d\d\d\d", str(data["locations"][x]))

@balwit
Copy link

balwit commented Apr 10, 2021

I ran into the same problem. I removed the dash but still had the problem. Any other hints for dealing with this. I am brand new to Python. I'd be happy to PayPal someone $20 bucks for successfully running this script on my JSON data.

@gifguide2code
Copy link
Author

gifguide2code commented Jan 8, 2022

Sorry for the late response troldmand and balwit! I was working with my data in mind when I wrote the script, and my JSON file only contains coordinates from North America. So my latitude (measured from the equator) is always positive, and my longitude (measured from the Prime Meridian in Great Britain) is always negative. That would explain why removing the dash was somewhat successful if you're hailing from somewhere on the other side of the Prime Meridian. It wouldn't work if you were trying anything south of the equator though, so I'm updating this a bit.

? in regex can be used to match 0 or 1 of whatever is immediately before it, so I'm thinking if we place a -? before the digit sequence, it'll catch a negative if it's there and nothing if it's not.

Hope that helps! Thanks for commenting--it would have never occurred to me that people from other parts of the globe would try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment