I am using the google distance matrix to determine the distance between zips, however, with the nature of the data sometimes there is not a legitimate zip code being passed to the API. Google still returns a distance. For example it was passed "BLVD" as the zip code and it returned 3000 miles. How can i tell google's api to throw an error if its not a real zip? I couldn't find any documentation on it...anything helps! thank you!
url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins={}&destinations={}&key={}"
# Get method of requests module
# return response object
r = requests.get(url.format(origin, dest, api_key))
# json method of response object
# return json format result
x = r.json()
If BLVD is passed to it the JSON return is
{'destination_addresses': ['205 SE Washington Blvd, Bartlesville, OK 74006, USA'],
'origin_addresses': ['Eagle Mountain, UT 84043, USA'],
'rows': [{'elements': [{'distance': {'text': '1,150 mi', 'value': 1850562},
'duration': {'text': '17 hours 25 mins', 'value': 62709}, 'status': 'OK'}]}],
'status': 'OK'}
and if a real zip is passed JSON return is
{'destination_addresses': ['Salt Lake City, UT 84116, USA'],
'origin_addresses': ['Eagle Mountain, UT 84043, USA'],
'rows': [{'elements': [{'distance': {'text': '42.4 mi', 'value': 68169},
'duration': {'text': '58 mins', 'value': 3496}, 'status': 'OK'}]}],
'status': 'OK'}