When I try to list json, it is returning the error:
save.append ((infos ['web_id']) TypeError: string indices must be integers
Can you help me understand how this happened? I have little experience in python and it's basically the first API I'm designing.
import requests
import psycopg2
key = 'KEY'
url = 'URL'
head = {'anystring':'KEY'}
r = requests.get(url, auth=('USER',key))
conn = psycopg2.connect("dbname='DBNAME' user='USER' host='HOST'
password='PASSWORD'")
insert = "INSERT INTO TABELA (web_id,name) VALUES"
info = r.json()
#print(info)
gravar=[]
for infos in info:
gravar.append((infos['web_id'],
infos['name']
) )
if len(gravar) >0 :
cur = conn.cursor()
y = b','.join(cur.mogrify("(%s,%s)", x) for x in gravar)
comando = insert + y.decode()
try:
cur.execute(comando)
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
cur.close()
print('Carga Completa')
else:
conn.close()
print('Nada a Inserir')