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?
>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]