PeakyBlinder
PeakyBlinder

Reputation: 1117

TypeError: string indices must be integers in json

I have a dictionary that contains a list like this

data = {'data': [0,0,4,279,14,46,10,5,
  0,0,1,257,13,44,10,5,
  1,1,254,0]}

When I am trying to convert this into json, it is giving me an error

json.dumps(data)['data']

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-94-ca909e16d3cc> in <module>
      6   0,0,1,257,13,44,10,5,
      7   1,1,254,0]}
----> 8 json.dumps(data)['data']

TypeError: string indices must be integers

Upvotes: 1

Views: 89

Answers (1)

jack
jack

Reputation: 281

You are converting your dictionary to string if you want to convert your JSON String back to a dict you have to use json.loads(json_string) A JSON Object is in almost every Language a String representation of an actual Javascript Object You can use it as a dict in Python but if you want it to export it you need to make string of it

Upvotes: 1

Related Questions