-6

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 3
    `api_data` is clearly a string, and e.g. `'foo'['bar']` would make no sense at all. – jonrsharpe Aug 10 '15 at 15:13
  • Take a look at the json library https://docs.python.org/2/library/json.html – blasko Aug 10 '15 at 15:14
  • can you expand jonrsharpe please – Christopher Jakob Aug 10 '15 at 15:14
  • jonrsharpe either offer useful advice on my problem or exit – Christopher Jakob Aug 10 '15 at 15:16
  • 1
    @ChristopherJakob don't be so rude. I *have* offered some useful advice, and the error message is telling you exactly what the problem is. *"I grab info from an API call that comes in the form of a dictionary"* is clearly a false assumption. – jonrsharpe Aug 10 '15 at 15:18
  • @ChristopherJakob As jon has already said, `api_data` is a `str` type, you cannot index out of it. You have to [convert it to a valid `dict`](https://stackoverflow.com/questions/19483351/converting-json-string-to-dictionary-not-list-python) – Cory Kramer Aug 10 '15 at 15:18
  • 3
    @ChristopherJakob what did I just say? [Be nice](http://stackoverflow.com/help/be-nice); you're the one coming here demanding that strangers help you when you can't even be bothered to correctly type the error message into the title box. Shape up, or ship out. – jonrsharpe Aug 10 '15 at 15:20
  • so it turns out that my var api_data is a dictionary ( I just did a type method on it) it seems these methods you guys have given me which I must say thank you for, may not apply. – Christopher Jakob Aug 10 '15 at 15:26
  • You wouldn't get that error message if `isinstance(api_data, dict)`; please provide a [mcve] that recreates the issue. You would probably find development and debugging easier with explicit parameters, rather than `global` variables. – jonrsharpe Aug 10 '15 at 15:31

1 Answers1

2

As jonsharpe already pointed out, your api_data is a string, which can only be indexed by integers.

Instead of using:

api_data = requests.get(apiEndpoint).json()

consider using json.load:

resp = requests.get(apiEndpoint)
api_data = json.loads(resp.text)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
DJanssens
  • 17,849
  • 7
  • 27
  • 42