I am trying to isolate several variables from a JSON file I have taken from https://api.weatherapi.com/v1/forecast.json?q=’s-Hertogenbosch&days=3&alerts=yes&aqi=yes&key=X
Key removed for safety reasons.
Therefore I use the following code:
import json
response = urlopen(url)
weer_data = json.loads(response.read())
location = weer_data['location']['name']
temp_current = weer_data['current']['temp_c']
alert_source = list(weer_data['alerts']['alert']['headline'])
location and temp_current can be isolated and stored as variable without any problem. But the alert_source gives me problems because there are multiple values with the same header ‘headline’ in the JSON file. The part that gives problems looks like this:
"alerts": {
"alert": [
{
"headline": "Koninklijk Meteorologisch Instituut van Belgiƫ",
"msgtype": "",
},
{
"headline": "Deutscher Wetterdienst",
"msgtype": "",
Anyone who can help me with storing the ‘headline’ as variable?
2
Answers
Try this code, I think it will work.
You can use a dict comprehension.