Reputation: 39
Suppose there is a list which looks like this
genre = '[{"id": 28, "name": "Action"}, {"id": 12, "name": "Adventure"}]'
When I checked type of this variable it showed string. I used
genre = genre.strip('][').split(', ')
but it became this
['{"id": 28', '"name": "Action"}', '{"id": 12', '"name": "Adventure"}']
as you can see the contents inside the list are still strings. I want them to be dictionaries
Upvotes: 0
Views: 47
Reputation: 1631
Barmar's comment answers it. This is what you need:
import json
genre = '[{"id": 28, "name": "Action"}, {"id": 12, "name": "Adventure"}]'
genre = json.loads(genre)
You can then access the dictionary elements with a loop:
for item in genre:
id = item['id']
name = item['name']
Upvotes: 2