Home > Software engineering >  How can i get value from json
How can i get value from json

Time:06-23

I am trying to get 'url' from json which i gain from responce in such way:

print('Please specifie the date in such way: YYYY-MM-DD.')
startdate = input()
enddate = input()
r = requests.get(f'https://api.nasa.gov/planetary/apod?api_key=Zex7CBAHQmbVfomUeIOyZXt9d8JccD4R50fNNhal&start_date={startdate}&end_date={enddate}')
parsed = r.json()
url = parsed['url']
print(url)

After i run program, I recive this message:

TypeError: list indices must be integers or slices, not str

I get JSON looks like this:

[{'date': '2022-06-08', 'explanation': "What are those unusual streaks? Some images of planet Earth show clear bright streaks that follow the paths of ships.  Known as ship tracks, these low and narrow bands are caused by the ship's engine
exhaust. Water vapor condenses around small bits of exhaust known as aerosols, which soon grow into floating water drops that efficiently reflect sunlight. Ship tracks were first discovered in 1965 in Earth images taken by NASA's TIROS satellites.  Multiple ship tracks are visible across the featured image that was captured in 2009 over the Pacific Ocean by the MODIS instrument on NASA's Terra satellite. Inspired by ship-tracks, some scientists have suggested deploying a network of floating buoys in the worlds' oceans that spray salt-aerosol containing sea-water into the air so that, with the help of the wind, streams of sunlight-reflecting clouds would also form.  Why do this?  These human-made clouds could reflect so much sunlight they might help fight global warming.    Today is: World
Oceans Day", 'hdurl': 'https://apod.nasa.gov/apod/image/2206/ShipTracks_Terra_4892.jpg', 'media_type': 'image', 'service_version': 'v1', 'title': 'Ship Tracks over the Pacific Ocean', 'url': 'https://apod.nasa.gov/apod/image/2206/ShipTracks_Terra_960.jpg'}, {'copyright': 'Wolfgang Zimmermann', 'date': '2022-06-09', 'explanation': "These cosmic clouds of gas and dust drift through rich star fields
along the plane of our Milky Way Galaxy toward the high flying constellation Cygnus. They're too faint to be seen with the unaided eye though, even on a clear,
dark night. Image data from a camera and telephoto lens using narrowband filters was used to construct this 10 degree wide field of view. The deep mosaic reveals a region that includes star forming dust clouds seen in silhouette against the characteristic glow of atomic hydrogen and oxygen gas. NGC 6888 is the standout emission nebula near the top. Blown by winds from a massive Wolf-Rayet star it's about 25 light-years across and known as the Crescent Nebula. A faint bluish curl just below center in the frame is also the signature of a Wolf-Rayet star. Burning fuel at a prodigious rate and near the end of their stellar lives, both stars will ultimately go out with a bang in a spectacular supernova explosion. Toward the right, a massive, young O type star powers the glow of Sh2-101, the Tulip Nebula.", 'hdurl': 'https://apod.nasa.gov/apod/image/2206/CygWideHa-OIIIBiColorImage2_crop2_2048.jpg', 'media_type': 'image', 'service_version': 'v1', 'title': 'Cosmic Clouds in Cygnus', 'url': 'https://apod.nasa.gov/apod/image/2206/CygWideHa-OIIIBiColorImage2_crop2_1024.jpg'}, {'copyright': 'Nicolas Rolland', 'date': '2022-06-10', 'explanation': "This colorful telescopic field of view features a trio of interacting galaxies almost 90 million light-years away, toward the constellation Virgo. On the right two spiky, foreground Milky Way stars echo the
extragalactic hues, a reminder that stars in our own galaxy are like those in distant island universes. With sweeping spiral arms and obscuring dust lanes, the
dominant member of the trio, NGC 5566, is enormous, about 150,000 light-years across. Just above it lies smaller, bluish NGC 5569. Near center a third galaxy, NGC 5560, is apparently stretched and distorted by its interaction with massive NGC 5566. The trio is also included in Halton Arp's 1966 Atlas of Peculiar Galaxies as Arp 286. Of course, such cosmic interactions are now appreciated as part of the evolution of galaxies.", 'hdurl': 'https://apod.nasa.gov/apod/image/2206/Arp286-202203-CDK24-FLIPL9000-LRGB_NicolasROLLAND_signature_LD.jpg', 'media_type': 'image', 'service_version': 'v1', 'title': 'Arp 286: Trio in Virgo', 'url': 'https://apod.nasa.gov/apod/image/2206/Arp286-202203-CDK24-FLIPL9000-LRGB_NicolasROLLAND_signature_LD1024.jpg'}]

I am new in python, how can i do it? Thanks all for answer.

CodePudding user response:

Your JSON is inside a list, so you need to select that first, before getting the attribute.

Try this:

url = parsed[0]['url']

CodePudding user response:

Looks like parsed is a list which has a dictionary in it. Try parsed[0]['url']

Alternatively, i'd suggest you look into the JSON module for python

  • Related