Skip to content Skip to sidebar Skip to footer

Python: Parse JSON In Loop

I have a function that pick-ups Country,City,Latitude,Longitude from a database and search on Yelp API for particular business. And everything works fine: def get_movietheaters_fo

Solution 1:

if 'businesses' in response_data:
   for SQL_element in response_data['businesses']:
       …

Solution 2:

response_data = response.json()

if response_data['error']['code'] == 'LOCATION_NOT_FOUND':
    # if nothing found, print a message and terminate the function
    print('No cinemas found.')
    return

# otherwise keep on going
sqlStatement = "INSERT INTO ..."

Solution 3:

response_data = response.json()
if response_data.get('error'):
  return  # Do nothing and return

Solution 4:

After response_data = response.json()

Verify if response_data Has a businesses key

If `businesses` in response_data.keys():
    # Do the code

Solution 5:

You should probably try to catch the error and abort further execution of the function.

if "error" in response_data:
    return #Function will stop on a return call

You could even return something like a status if you want, e.g.

return True 
return False

See this question for more information on the return command.


Post a Comment for "Python: Parse JSON In Loop"