I\'m having trouble trying to get a list of values from a specific key inside an json array using python. Using the JSON example below, I am trying to create a list which co
You cannot do contents[:]["name"]
since contents
is a list is a dictionary with integer indexes, and you cannot access an element from it using a string name
.
To fix that, you would want to iterate over the list and get the value for key name
for each item
import json
contents = []
try:
with open("./simple.json", 'r') as f:
contents = json.load(f)
except Exception as e:
print(e)
li = [item.get('name') for item in contents]
print(li)
The output will be
['Bulbasaur', 'Ivysaur']