Reading the JSON data from api in Python

I am getting error in this code as I am unable to read the JSON data properly

import requests
from operator import itemgetter
import json
import webbrowser
url = 'https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?earth_date='
apikey=('DEMO_KEY')
earth_date = input('please enter the required date in yyyy-m-dd format')
URL = (url + earth_date +'&api_key=' + apikey)
print(URL)
r =requests.get(URL)
print('Status code:',r.status_code)
response_dict = json.loads(r.text)
response_dict = r.json()
for rover_data in response_dict:
    print('image url:',rover_data['img_src'])
webbrowser.open(rover_data['img_src'])

This the error I am getting

please enter the required date in yyyy-m-dd format2015-6-11
https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?earth_date=2015-6-11&api_key=DEMO_KEY
Status code: 200
Traceback (most recent call last):
  File "d:\Python Programs\rover data.py", line 15, in <module>
    print('image url:',rover_data['img_src'])
TypeError: string indices must be integers

can anyone please help me?

Take a closer look, at what is rover_data.

I think I am storing the list in rover_data from the response_dict dictionary.

Have you checked if that’s what actually is happening?

the values when read are not storing and failing to return the data when asked for.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.