I grab info from an API call that comes in the form of a dictionary. I need to grab a specific key and set a variable to it. Easy enough but observe. The code below is only to the processes involved.
totalEntries = 0 # global var
api_data = requests.get(apiEndpoint).json()
def populateVars():
global totalEntries
totalEntries = api_data['total_entries']
output of the api_data
just for giggles:
{u'per_page': 100, u'total_entries': 1, u'current_page': 1, `u'restaurants': [{u'city': u'Abilene', u'reserve_url': u'http://www.opentable.com/single.aspx?rid=152869', u'name': u'Copper Creek Fine Texas Dining', u'area': u'Dallas - Fort Worth', u'country': u'US', u'price': 2, u'phone': u'3256924424', u'state': u'TX', u'postal_code': u'79602', u'address': u'4401 Loop 322', u'lat': 32.397913, u'image_url': u'https://www.opentable.com/img/restimages/152869.jpg', u'lng': -99.716776, u'mobile_reserve_url': u'http://mobile.opentable.com/opentable/?restId=152869', u'id': 152869}]}`
and again the error:
totalEntries = api_data['total_entries']
TypeError: string indices must be integers, not str
Why is this happening? What is the fix?