I want to extract information from .json format through API for each element in array.
I tried to use below code but get me an error.
# Get the response from the API endpoint.
response = requests.get("http://api.open-notify.org/astros.json")
data = response.json()
print(data["people"][0:2]["name"])
I would expect to see every name listed instead of an error:
TypeError: list indices must be integers or slices, not list
I know there is error in [O:2] array. Can anyone help ?
name
, try runningnames = [di['name'] for di in data["people"][0:2]]
, herenames
will store list(array) ofnames
that you're to get. – Naman Chikara Jul 8 '19 at 9:02