Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parse a list in Python that might have whitespace and returning JSONDecodeError: Extra data: line 1 column 6 (char 5) errors?

I have the following list in Python3

mylist=['"{  "score": "Really Good",  "text": "This is a text accompanying the score, level, and more.",  "level": "high level"}"',
 '"{  "score": "Not So Good",  "text": "This is a text accompanying the score, level, and more.",  "level": "high level"}"']

I would like to access each of the elements in mylist, such as score,key,text. When I try to do

scores = [json.loads(json_str)["score"] for json_str in mylist]

I get the error JSONDecodeError: Extra data: line 1 column 6 (char 5). My suspicion is that it is because of whitespace, and more (single quotes on outside). How can I parse this correctly?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It looks like you have an extra set of double quotes in each string. You can either remove them manually or just slice them out like this:

scores = [json.loads(json_str[1:-1])["score"] for json_str in mylist]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading