I have a problem with my code
response = requests.get("http://www.testecliente.com.br/ApiTeste/GetDadosCartaoCliente?CL_COD=1" )
retorno = response.content
retorno = retorno.decode("utf-8")
json_data = json.loads(retorno)
CL_NOME = json_data['CL_NOME']
print ("NOME=" + CL_NOME)
When I try to run the following error occurs :
CL_NOME = json_data['CL_NOME']
TypeError : string indices must be integers
if I send print json_data appears:
{"CC_COD":16,"CL_COD":40,"CC_NUMERO_CARTAO":"68453217628884","CC_BANDEIRA":"visa","CC_VALIDADE":"09/21","CC_COD_SEGURANCA":"188","CC_FUNCAO":"C","CC_PRINCIPAL":"S","CL_NOME":"Alexandre Luiz"}
if I print the type the result is:
print (type(json_data))
result --> <class 'str'>
Can someone help me ?
What does the following output?
response = requests.get("http://www.testecliente.com.br/ApiTeste/GetDadosCartaoCliente?CL_COD=1" )
retorno = response.content
print(retorno)
retorno = retorno.decode("utf-8")
print(retorno)
Use
code tag
Don't use decode of
json.load()
with Requests,theses are build into Requests.
Example:
import requests
url = 'https://api.github.com/'
url_get = requests.get(url)
json_data = url_get.json()
print(url_get.encoding)
print(type(json_data))
# Parse json
print(json_data["current_user_url"])
Output:
utf-8
<class 'dict'>
https://api.github.com/user
Error:
TypeError : string indices must be integers
That's a common error message because in a larger json usually also a get back list in the dictionary.
So it normal to parse like this.
json_data['something']
[0]
['CL_NOME']
(Jan-10-2018, 02:38 PM)mpd Wrote: [ -> ]What does the following output?
response = requests.get("http://www.testecliente.com.br/ApiTeste/GetDadosCartaoCliente?CL_COD=1" )
retorno = response.content
print(retorno)
retorno = retorno.decode("utf-8")
print(retorno)
retorno = response.content
print(retorno)
b'"[{\\"CC_COD\\":16,\\"CL_COD\\":40,\\"CC_NUMERO_CARTAO\\":\\"4984532174628184\\",\\"CC_BANDEIRA\\":\\"visa\\",\\"CC_VALIDADE\\":\\"09/21\\",\\"CC_COD_SEGURANCA\\":\\"188\\",\\"CC_FUNCAO\\":\\"C\\",\\"CC_PRINCIPAL\\":\\"S\\",\\"CL_NOME\\":\\"Alexandre Gontijo\\"}]"'
retorno = retorno.decode("utf-8")
print(retorno)
"[{\"CC_COD\":16,\"CL_COD\":40,\"CC_NUMERO_CARTAO\":\"4984532174628184\",\"CC_BANDEIRA\":\"visa\",\"CC_VALIDADE\":\"09/21\",\"CC_COD_SEGURANCA\":\"188\",\"CC_FUNCAO\":\"C\",\"CC_PRINCIPAL\":\"S\",\"CL_NOME\":\"Alexandre Gontijo\"}]"
The value returns in bytes by what I do retorno = retorno.decode("utf-8")
No encoding,as mention so is Requests doing that.
If the url return json,then this should work.
import requests
response = requests.get("http://www.testecliente.com.br/ApiTeste/GetDadosCartaoCliente?CL_COD=1")
# Get json,a json encode/decode is build in
json_data = response.json()
# Should be a dictionary
print(type(json_data_data))
# Look at data
print(json_data)
snippsat called it. You have a list of objects, not just an object. You just need to access the first (0) element in the array:
CL_NOME = json_data[0]['CL_NOME']
(Jan-10-2018, 05:14 PM)mpd Wrote: [ -> ]snippsat called it. You have a list of objects, not just an object. You just need to access the first (0) element in the array:
CL_NOME = json_data[0]['CL_NOME']
I did it this way and the same error happens
The problem is that the type of the variable continues as a string even after converting to JSON
json_data = response.json()
print(type(json_data_data))
result : <class 'str'>
(Jan-10-2018, 06:10 PM)ALEGON Wrote: [ -> ] (Jan-10-2018, 05:14 PM)mpd Wrote: [ -> ]snippsat called it. You have a list of objects, not just an object. You just need to access the first (0) element in the array:
CL_NOME = json_data[0]['CL_NOME']
I did it this way and the same error happens
The problem is that the type of the variable continues as a string even after converting to JSON
json_data = response.json()
print(type(json_data_data))
result : <class 'str'>
I tried the URL in your original post but get a 404 error. Snippsat's sample is a little different from yours and if the server is returning the data correctly, you should get an object with
import requests
response = requests.get(URL)
obj = response.json()
Are you sure the code below is the one which is causing the error?
response = requests.get("http://www.testecliente.com.br/ApiTeste/GetDadosCartaoCliente?CL_COD=1" )
retorno = response.content
retorno = retorno.decode("utf-8")
json_data = json.loads(retorno)
CL_NOME = json_data['CL_NOME']
print ("NOME=" + CL_NOME)
I'm asking because in your GET request you are sending
CL_COD=1, but when you say you print json_data it shows
CL_COD = 40
If you place the entire code, might be helpfull