I have the following data:
d = "{\"key_1": \" val_1\", \"key_2\": \"val_2\"}{\"key_3\": \" val_3\", \"key_4\": \"val_4\"}"
which I would like to translate to a list of dictionary, e.g.
d_list = [{\"key_1": \" val_1\", \"key_2\": \"val_2\"}, {\"key_3\": \" val_3\", \"key_4\": \"val_4\"}]
- json.loads(d) gives me error of type: raise ValueError(errmsg(“Extra data”, s, end, len(s)))
Any suggestions?
You can use
JSONDecoderand itsraw_decode()method to accomplish this.raw_decode()will read a complete JSON object, and return a tuple whose first member is the object, and whose second is the offset into the string where the decoder stopped reading.Basically, you need to read one object, then store it in an array, then read the next object from the string, and so on until you are at the end of the string. Like this:
Which will output this: