As a beginner in Typescript, I am eager to create a straightforward weather application using Firebase functions. One of the initial steps involves making an API call to fetch the current temperature of a particular city.
Upon making the API call, the JSON response provided includes various details such as latitude, longitude, timezone, and most importantly, the current weather data containing temperature information.
{
"latitude": 40.710335,
"longitude": -73.99307,
"generationtime_ms": 0.3579854965209961,
"utc_offset_seconds": 0,
"timezone": "GMT",
"timezone_abbreviation": "GMT",
"elevation": 27.0,
"current_weather": {
"temperature": 12.3,
"windspeed": 14.0,
"winddirection": 181.0,
"weathercode": 3,
"time": "2023-01-13T09:00"
},
"hourly_units": {
"time": "iso8601",
"temperature_2m": "°C"
},
In order to extract only the temperature value from the current weather section of the JSON response, I have been working on the following code snippet:
export const getWeather = functions.https.onRequest(async (request, response) => {
const dataResponse = await fetch("https://api.open-meteo.com/v1/forecast?latitude=40.71&longitude=-74.01&hourly=temperature_2m¤t_weather=true");
const data = await dataResponse.json();
console.log(data.current_weather.temperature); // This line retrieves the temperature value
response.send(data.current_weather.temperature); // Sending only the temperature value in the response
});
I am exploring ways to efficiently extract and utilize specific values like temperature from complex JSON responses generated by API calls.