Harry BM
Harry BM

Reputation: 27

How to get multiple data from json list in python

I'm new to python

I'm attempting to use the Hans volcano api to produce a map (https://volcanoes.usgs.gov/hans2/apiv2/volcanoApi/allWithNotice)

When printing my URL as json I am presented with many volcanoes in this format

[{'obsAbbr': 'avo', 'volcCd': 'ak6', 'volcName': 'Akutan', 'volcUrl': 'https://avo.alaska.edu/volcanoes/volcinfo.php?volcname=Akutan', 'vnum': '311320', 'imgUrl': 'https://avo.alaska.edu/images/dbimages/display/1108076476_60_3.jpg', 'threat': 'Very High Threat'},

From this I'm looking to extract volcano names. When making my dictionary I'm given one volcano only. I'm looking to obtain all the volcano names from this json file.

v = requests.get(url2.format()).json()

volcano = {
    'name' : v[0]['volcName'],
}

Any help would be appreciated, cheers

Upvotes: 0

Views: 55

Answers (3)

Tom McLean
Tom McLean

Reputation: 6327

I wouldn't keep it as a list of dictionaries, just use a dictionary:

volcano_threats = {x['volcName']: x['threat'] for x in v}

Output:

{'Akutan': 'Very High Threat',
 'Alaskan Volcanoes': None,
 'Aniakchak': 'High Threat',
 'Atka volcanic complex': 'High Threat',
...
}

Or to keep all the data:

volcanos = {x['volcName']: x for x in v}

Upvotes: 1

Rodrigo Rodrigues
Rodrigo Rodrigues

Reputation: 8556

You can iterate over your data and generate a list with remapped dicts, all easily with a list comprehension syntax. Like this:

v = requests.get(url2.format()).json()
volcanoes = [dict(name=x['volcName'], threat=x['threat']) for x in v]

# [{'name': 'Akutan', 'threat': 'Very High Threat'},
#  {'name': 'Alaskan Volcanoes', 'threat': None},
#  {'name': 'Aniakchak', 'threat': 'High Threat'},
#  {'name': 'Atka volcanic complex', 'threat': 'High Threat'}, ...
# ]

Upvotes: 0

Rahul K P
Rahul K P

Reputation: 16081

You could do like this,

v = requests.get(url2.format()).json()
print([i['volcName'] for i in v])

Results

['Akutan', 'Alaskan Volcanoes', 'Aniakchak', 'Atka volcanic complex', 'Augustine', 'Bogoslof', 'Cleveland', 'Davidof', 'Dutton', 'Edgecumbe', 'Fourpeaked', 'Frosty', 'Gareloi', 'Great Sitkin', 'Griggs', 'Iliamna', 'Kanaga', 'Kasatochi', 'Katmai', 'Korovin', 'Little Sitkin', 'Mageik', 'Makushin', 'Martin', 'Novarupta', 'Okmok', 'Pavlof', 'Redoubt', 'Sanford', 'Semisopochnoi', 'Shishaldin', 'Snowy Mountain', 'Spurr', 'Takawangha', 'Tanaga', 'Trident', 'Ugashik-Peulik', 'Ukinrek Maars', 'Veniaminof', 'Westdahl', 'Wrangell', 'Coso Volcanic Field', 'Lassen Volcanic Center', 'Long Valley Caldera', 'Cascade Range', 'Mount Hood', 'Mount St. Helens', 'Newberry', 'Three Sisters', 'Haleakala', 'Hualalai', "Kama'ehuakanaloa", 'Kilauea', 'Mauna Kea', 'Mauna Loa', 'Ofu-Olosega', "Ta'u Island", 'Tutuila Island', 'Agrigan', 'Ahyi Seamount', 'Anatahan', 'Pagan', 'Sarigan', 'Yellowstone']

Edit

If you have more data to fetch,

[{'name':i['volcName'], 'threat':i['threat']} for i in v]

# result
[{'name': 'Akutan', 'threat': 'Very High Threat'},
 {'name': 'Alaskan Volcanoes', 'threat': None},
 {'name': 'Aniakchak', 'threat': 'High Threat'},
 {'name': 'Atka volcanic complex', 'threat': 'High Threat'},
 {'name': 'Augustine', 'threat': 'Very High Threat'},
....

Upvotes: 1

Related Questions