Python program to fetch current weather data from the JSON file

10.b) Write a python program to fetch current weather data from the JSON file

import json
# Load the JSON data from file
with open('weather_data.json') as f:
 data = json.load(f)
# Extract the required weather data
current_temp = data['main']['temp']
humidity = data['main']['humidity']
weather_desc = data['weather'][0]['description']
# Display the weather data
print(f"Current temperature: {current_temp}°C")
print(f"Humidity: {humidity}%")
print(f"Weather description: {weather_desc}")

JSON FILE :

{
"weather": [
 {
 "id": 800,
"main": "Clear",
 "description": "clear sky",
 "icon": "01d"
 }
 ],
"base": "stations",
 "main": {
 "temp": 15.45,
 "humidity": 64
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *