How to Extract Specific Data from a Weather API Response in Python?

0
13
Asked By CuriousCoder42 On

I'm working with a weather API and have successfully pulled the data. Here's the response I got:

```json
{
'location': {
'name': 'London',
'region': 'City of London, Greater London',
'country': 'United Kingdom',
'lat': 51.5171,
'lon': -0.1062,
'tz_id': 'Europe/London',
'localtime_epoch': 1769698873,
'localtime': '2026-01-29 15:01'
},
'current': {
'last_updated_epoch': 1769698800,
'last_updated': '2026-01-29 15:00',
'temp_c': 7.2,
'temp_f': 45.0,
'is_day': 1,
'condition': {
'text': 'Partly cloudy',
'icon': '//cdn.weatherapi.com/weather/64x64/day/116.png',
'code': 1003
},
'wind_mph': 9.4,
'wind_kph': 15.1,
'wind_degree': 114,
'wind_dir': 'ESE',
'pressure_mb': 995.0,
'pressure_in': 29.38,
'precip_mm': 0.0,
'precip_in': 0.0,
'humidity': 76,
'cloud': 25,
'feelslike_c': 4.4,
'feelslike_f': 40.0,
'windchill_c': 3.3,
'windchill_f': 38.0,
'heatindex_c': 6.3,
'heatindex_f': 43.4,
'dewpoint_c': 1.9,
'dewpoint_f': 35.4,
'vis_km': 10.0,
'vis_miles': 6.0,
'uv': 0.2,
'gust_mph': 12.5,
'gust_kph': 20.1,
'short_rad': 194.92,
'diff_rad': 94.54,
'dni': 493.35,
'gti': 0.0
}
}
```

Now, can someone guide me on how to extract the 'temp_c' value from this structure?

4 Answers

Answered By HelpfulHarry21 On

To get the temperature in Celsius (`temp_c`), you can access it like this:

```python
weather_data = { ... } # your data here

temp_c = weather_data['current']['temp_c']
print(temp_c) # This will print the value 7.2
```
Just make sure you replace `weather_data` with your actual variable that holds the API response.

Answered By InquisitiveIvy On

Just so you know, you don’t need to dive deep into everything at once. Mastering the basics of accessing dictionary items will help you a lot with JSON data like what you're dealing with.

Answered By CodeGuru99 On

You’re on the right track! Since `temp_c` is nested inside the `current` dictionary, use this access pattern:

```python
temperature_c = data['current']['temp_c']
print(temperature_c)
```
This way, you'll directly get the Celsius temperature you want!

Answered By SkepticalSam On

Honestly, if you're stuck on this, you might want to brush up on how to work with dictionaries in Python. It's a pretty fundamental skill. You can easily find tutorials online!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.