I have a json file which looks like https://www.dropbox.com/s/raplkyl5le1rmh9/testDehil.json?dl=0
My task was to read the address and find the full specific geo-code and update back the json file. I am able to do the following till now:
--> Read the jsonfile and pass the address to Google API to get the exact latitute and longitude.
I am stuck at this point where i need to update the read file to the newly received exact latitude and longitude.
My code is :
import json
import requests
import time
import shutil
import os
from pprint import pprint
def open_json():
with open('/home/nishant/Documents/Python/Gnitin/testDehil.json') as json_data:
d = json.load(json_data)
#json_data.close()
#pprint(d)
for item in d:
a = item['address']['address']
google_call(a)
time.sleep(10)
# EXTRACT THE address from each and pass it below: ##
def google_call(add):
address = add #This needs to be run in loop
api_key = "XXXXXXXXXXXXXXXXXXXXX"
api_response = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}'.format(address, api_key))
api_response_dict = api_response.json()
if api_response_dict['status'] == 'OK':
latitude = api_response_dict['results'][0]['geometry']['location']['lat']
longitude = api_response_dict['results'][0]['geometry']['location']['lng']
print 'Latitude:', latitude
print 'Longitude:', longitude
write_back_cool_stuff(latitude,longitude)
def write_back_cool_stuff(lat,lon):
## How to write back to the json file with received lat and long ?
if __name__ == '__main__':
source = "/home/nishant/Documents/Python/Gnitin/testDehil.json"
dest = "/home/nishant/Documents/Python/Gnitin/testDehil_clean.json"
shutil.copyfile(source,dest)
open_json()