0
I’m beginner and I’m caught in a problem I don’t even know how to research right.
This is what happens: My code needs to select data from a database table (so far so good), and then need to separate the data into other variables as I will mount a json to send the information to dynamoDB.
I need to know how to extract the information from my variable that received all data from my table.
And if possible, how do I send my json all lines of the same.
If anyone has at least some material for me to read and have a north, it would be very helpful. Thank you!
#######################################################
####ETAPA - BUSCANDO INFORMAÇÕES NO FIREBIRD########
#######################################################
print('ETAPA - BUSCANDO INFORMAÇÕES NO FIREBIRD')
##Executar select
try:
sql = "SELECT * FROM USUARIO"
cursor.execute(sql);
USUARIO = cursor.fetchall()
except Exception as e:
print('')
finally:
print(USUARIO)
somedict = {"USU_ID": [x[0] for x in USUARIO],
"USU_NOME": [x[0] for x in USUARIO],
"USU_USUARIO": [x[0] for x in USUARIO]
}
print(somedict)
#######################################################
####SEPARANDO INFORMAÇÕES PARA CRIAR ARQUIVO JSON########
#######################################################
USU_ID = somedict[USU_ID]
USU_NOME = somedict[USU_NOME]
USU_USUARIO = somedict[USU_USUARIO]
###################################################
##ETAPA - CHAMANDO API PARA POST##################
###################################################
#print('MONTANDO ARQUIVO JSON PARA POST')
payload = {
"USU_ID":USU_ID,
"USU_NOME":USU_NOME,
"USU_USUARIO":USU_USUARIO
}
print(str(payload))
try:
ENVINDO POST PARA API
url = 'https://l.execute-api.us-east-2.amazonaws.com/dev/gravar'
print('URL API Post: ', url)
headers = {
"Content-Type": "application/json",
'Accept': 'application/json'
}
response = requests.post(url, json=payload, headers=headers)
except Exception as e:
print('ERRO ac chamar API para o POST')
finally:
print('O POST foi bem sucedido')
print(response.text)
Can show what
USUARIO
returns you? So it’s easier to test your code. Another thing: why do you use exceptions just to print something on the screen? And more: why useException as e
if you don’t use thee
?– Murilo Sitonio
Which JSON you need to send?
– Woss
@Murilositonio I am reusing a code, so it has the 'Exception as' without the 'e'. I print on the screen to understand what is happening in the code rs.
– Rodolfo Sousa
@Andersoncarloswoss then, was reading and it seems that to transform my variable into a json array (n know if this makes sense), would need to transform this tuple into a list.. JSON model is one I learned and tested to impute in dynamoby. I don’t know if I answered correctly what you questioned...
– Rodolfo Sousa
@Andersoncarloswoss maybe this will help you understand, https://stackoverflow.com/questions/50000536/convert-list-of-objects-to-json-array, the guy who posted this, in the middle of the question has the json matrix model that I would need, because it should be a json for each row of my table.
– Rodolfo Sousa