I am trying to retrieve some data from API and put it into database but I have faced some problems.
Data from above URL
[
{"country":"Poland","country_code":"PL","last_updated":"2020-11-02T00:01:00.000Z","new_deaths":92,"new_infections":15578,"new_recovered":7818},
{"country":"Poland","country_code":"PL","last_updated":"2020-11-03T00:02:00.000Z","new_deaths":227,"new_infections":19364,"new_recovered":5573},
{"country":"Poland","country_code":"PL","last_updated":"2020-11-04T00:00:00.000Z","new_deaths":373,"new_infections":24692,"new_recovered":8974},
{"country":"Poland","country_code":"PL","last_updated":"2020-11-05T00:11:00.000Z","new_deaths":367,"new_infections":27143,"new_recovered":8721}
]
Error
Date = i['last_updated'] TypeError: string indices must be integers
Part of my code
try:
response = http.request('GET', url)
data = json.loads(response.data.decode('utf-8'))
index = 0
for i in data:
Date = None
Confirmed = None
Deaths = None
Recovered = None
#Active = None
Date = i['last_updated']
Confirmed = i['new_infections']
Deaths = i['new_deaths']
Recovered = i['new_recovered']
#Active = i['Active']
cur.execute("""
INSERT INTO covid_stats_poland
VALUES (%s, %s, %s, %s, %s);
""",
(index, Date, Confirmed, Deaths, Recovered))
conn.commit()
index += 1
cur.close()
Can someone please explain what am I doing wrong / how I can fix it?