![]() |
Error string indice must integers Json - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Error string indice must integers Json (/thread-7436.html) |
Error string indice must integers Json - ALEGON - Jan-10-2018 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 ? RE: Error string indice must integers Json - mpd - Jan-10-2018 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) RE: Error string indice must integers Json - snippsat - Jan-10-2018 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"])
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']
RE: Error string indice must integers Json - ALEGON - Jan-10-2018 (Jan-10-2018, 02:38 PM)mpd Wrote: What does the following output? 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") RE: Error string indice must integers Json - snippsat - Jan-10-2018 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) RE: Error string indice must integers Json - mpd - Jan-10-2018 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'] RE: Error string indice must integers Json - ALEGON - Jan-10-2018 (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: 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'> RE: Error string indice must integers Json - mpd - Jan-10-2018 (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: 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() RE: Error string indice must integers Json - lghauth - Jan-10-2018 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 |