Python Forum
Error string indice must integers Json
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error string indice must integers Json
#1
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 ?
Reply
#2
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)
Reply
#3
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']
Reply
#4
(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")
Reply
#5
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)
Reply
#6
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']
Reply
#7
(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'>
Reply
#8
(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()
Reply
#9
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  json loads throwing error mpsameer 8 711 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,277 Jun-09-2023, 06:56 PM
Last Post: kpatil
  [split] Parse Nested JSON String in Python mmm07 4 1,548 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,435 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  json decoding error deneme2 10 3,680 Mar-22-2023, 10:44 PM
Last Post: deanhystad
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,051 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,474 Dec-30-2022, 05:38 PM
Last Post: dee
  python requests library .JSON() error mHosseinDS86 6 3,453 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  Read nested data from JSON - Getting an error marlonbown 5 1,383 Nov-23-2022, 03:51 PM
Last Post: snippsat
  TypeError: string indices must be integers JonWayn 12 3,412 Aug-31-2022, 03:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020