0

While working with a REST API, I am getting an error like this.

TypeError string indices must be integers

My code:

response = table.get_item(
    Key={'facility_id': str(facility_id)}
)
if "Item" in response.keys():
    returnvals['status'] = 1
    item = response['Item']
    for item in response["Item"]:
        result.append({"facility_id": item["facility_id"],
        result.append({"facility_id": item["facility_id"],
                       "facility_name": item["facility_name"],
                       "display_units": item["display_units"],
                       "created_by": item["created_by"],
                       "created_date": item["created_date"],
        })

    returnvals['params'] = result

The line on which I am getting error is

 result.append({"facility_id": item["facility_id"],

What's causing this?

CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
Karthik
  • 47
  • 3
  • 2
    What is the type of `item`? – drum Dec 31 '20 at 05:09
  • 2
    Does this answer your question? [Why am I seeing "TypeError: string indices must be integers"?](https://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers) – CrazyChucky Dec 31 '20 at 05:09
  • it is dictionary – Karthik Dec 31 '20 at 05:10
  • Does `response["Item"]` return a list of items? Or is it a dictionary? – Svetlana Levinsohn Dec 31 '20 at 05:15
  • `item = response['Item']` doesn't really do anything if you then go on to reassign `item` to each element *in* `response['Item']`. – CrazyChucky Dec 31 '20 at 05:27
  • item is probably a string. Unless its a dict or json (in context of requests) the above thing would not work. – Its not blank Dec 31 '20 at 06:41
  • 1
    You *think* `item` is a `dict`. The interpreter says it is a string. In such cases the sensible thing to do is print `item` to find out why the interpreter disagrees with you. It is correct, whatever you may think. – BoarGules Dec 31 '20 at 07:02

1 Answers1

0

The error above :

TypeError string indices must be integers

and as you have mentioned in your question that the error is in the line below

result.append({"facility_id":item["facility_id"],

Hence, I guess that item is a string here. However, you treat it as a dictionary object and that is causing the issue.