close

[Solved] TypeError: string indices must be integers while parsing JSON using Python?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TypeError: string indices must be integers while parsing JSON using Python? in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TypeError: string indices must be integers while parsing JSON using Python Error Occurs?

Today I get the following error TypeError: string indices must be integers while parsing JSON using Python? in python.

How To Solve TypeError: string indices must be integers while parsing JSON using Python Error ?

  1. How To Solve TypeError: string indices must be integers while parsing JSON using Python Error ?

    To Solve TypeError: string indices must be integers while parsing JSON using Python ErrorTry replacing j = json.loads(json.dumps(jsonStr)) with j = json.loads(jsonStr).

  2. TypeError: string indices must be integers while parsing JSON using Python?

    To Solve TypeError: string indices must be integers while parsing JSON using Python ErrorTry replacing j = json.loads(json.dumps(jsonStr)) with j = json.loads(jsonStr).

Solution 1

Try replacing j = json.loads(json.dumps(jsonStr)) with j = json.loads(jsonStr).

Solution 2

The problem is that jsonStr is a string that encodes some object in JSON, not the actual object.

You obviously knew it was a string, because you called it jsonStr. And it’s proven by the fact that this line works:

jsonStr = data.decode("utf-8")

So, jsonStr is a string. Calling json.dumps on a string is perfectly legal. It doesn’t matter whether that string was the JSON encoding of some object, or your last name; you can encode that string in JSON. And then you can decode that string, getting back the original string.

So, this:

j = json.loads(json.dumps(jsonStr))

… is going to give you back the exact same string as jsonStr in j. Which you still haven’t decoded to the original object.

To do that, just don’t do the extra encode:

j = json.loads(jsonStr)

If that isn’t clear, try playing with it an interactive terminal:

>>> obj = ['abc', {'a': 1, 'b': 2}]
>>> type(obj)
list
>>> obj[1]['b']
2
>>> j = json.dumps(obj)
>>> type(j)
str
>>> j[1]['b']
TypeError: string indices must be integers
>>> jj = json.dumps(j)
>>> type(jj)
str
>>> j
'["abc", {"a": 1, "b": 2}]'
>>> jj
'"[\\"abc\\", {\\"a\\": 1, \\"b\\": 2}]"'
>>> json.loads(j)
['abc', {'a': 1, 'b': 2}]
>>> json.loads(j) == obj
True
>>> json.loads(jj)
'["abc", {"a": 1, "b": 2}]'
>>> json.loads(jj) == j
True
>>> json.loads(jj) == obj
False

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read