https://i.sstatic.net/OQZvD.pngGreetings! I am currently facing an issue while trying to retrieve JSON data from an API. Whenever I attempt to extract data from the API, everything works smoothly except for when I try to access the distance value, which results in undefined. Any assistance would be greatly appreciated.
Here is the code snippet:
fetch('https://maps.googleapis.com/maps/api/distancematrix/json?units=meter&origins=Gurd%20Shola%201%20Station,%20Addis%20Ababa&destinations=Bole%20Medhane%20Alem%20Church,%20Addis%20Ababa&key=API_KEY')
.then(response => {
return response.json()
})
.then(data => {
// Work with JSON data here
console.log(data.rows.elements.distance.value)
})
.catch(err => {
})
The retrieved result from the web appears as follows:
{
"destination_addresses" : [ "Addis Ababa, Ethiopia" ],
"origin_addresses" : [ "Addis Ababa, Ethiopia" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "6.4 km",
"value" : 6386
},
"duration" : {
"text" : "15 mins",
"value" : 901
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}