TypeError: string indices must be integers in json

Question:

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
Asked By: PeakyBlinder

||

Answers:

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

Answered By: jack
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.