I have been using the following to append information to a JSON response.
for result in results:
if result["status"] == "OK":
for route, origincoord in izip(results, origincoords):
route['routes'][0]['legs'][0][u'_sent_origin'] = origincoord
Where origincoords is a list of coordinates. The number of origincoords is equal to the number of JSON dictionaries I have.
This works fine assuming the JSON response returned is valid (ie "Status": 'OK').
However, if for whatever reason it is not (for example, Unknown_Error, Not_Found etc) the above will fail as the relevant origincoords cannot be paired with their relevant JSON response. 1. the integrity of the data is lost and 2. there isnt an equal amount of JSON dictionaries to origincoords thus an index error is raised and 3.the location to insert the data is not possible with the path given ['routes'][0]['legs'][0][u'_sent_origin'] as it wont exist when a status != "OK" is returned.
I have tried the following to overcome this -
for result in results:
for route, origincoord in izip(results, origincoords):
if result["status"] == "OK":
route['routes'][0]['legs'][0][u'_sent_origin'] = origincoord
if result["status"] != "OK":
route[u'_sent_origin'] = origincoord
However this also returns an IndexError
route['routes'][0]['legs'][0][u'_sent_origin'] = origincoord
IndexError: list index out of range
How do I maintain the integrity of the appended information to their JSON responses in the scenario of a JSON response with an error?
EDIT. My desired outcome.
In this example, the first result has an unknown error, the second is OK and the third has an unknown error.
origincoords = ['51.51964085,-0.092434321',
'51.51963442,-0.092433965',
'51.52208762,-0.095990014']
results = [{u'routes': [], u'status': u'UNKNOWN_ERROR'},
{u'routes': [{u'bounds': {u'northeast': {u'lat': value,
u'lng': value},
u'southwest': {u'lat': value,
u'lng': value}},
u'copyrights': u'value',
u'overview_polyline': {u'points': u’value’},
u'summary': u’value’,
u'warnings': [],
u'waypoint_order': []}],
u'status': u'OK'},
{u'routes': [], u'status': u'UNKNOWN_ERROR'}]
I would like to pair the first set of origincoords to the first JSON response, the second to the second JSON etc.
Thus
results = [{u'routes': [], u'status': u'UNKNOWN_ERROR', u'_sent_origin': '51.51964085,-0.092434321'},
{u'routes': [{u'bounds': {u'northeast': {u'lat': value,
u'lng': value},
u'southwest': {u'lat': value,
u'lng': value}},
u'copyrights': u'value',
u'overview_polyline': {u'points': u’value’},
u'summary': u’value’,
u'warnings': [],
u'waypoint_order': []}],
u'status': u'OK',
u'_sent_origin': '51.51963442,-0.092433965'}],
{u'routes': [], u'status': u'UNKNOWN_ERROR', u'_sent_origin': '51.52208762,-0.095990014'}]
Your complete set-up is not clear to me from the snippet you posted, but it seems you need to check the status before iterating over the izip
.
for result in results:
if result["status"] == "OK":
for route, origincoord in izip(results, origincoords):
route['routes'][0]['legs'][0][u'_sent_origin'] = origincoord
else
for route, origincoord in izip(results, origincoords):
route[u'_sent_origin'] = origincoord
I was doing two loops instead of one!
Solution -
for route, origincoord in izip(results, origincoords):
if route["status"] == "OK":
route['routes'][0]['legs'][0][u'_sent_origin'] = origincoord
else:
route[u'_sent_origin'] = origincoord
I didn't need to do two loops. I was doing 'for result in results' and 'for route, origincoord in izip(results, originscoords):'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.