Can't retrieve JSON value

I am not quite sure what I am doing wrong, I have called the distancematrix API and I retrieve this value as a result:-
{
“destination_addresses”:[“Werrikimbe NSW 2446, Australia”],
“origin_addresses”:[“Melbourne VIC 3000, Australia”],
“rows”:[{“elements”:[{“distance”:{“text”:“1,295 km”,“value”:1294637},“duration”:{“text”:“13 hours 8 mins”,“value”:47279},“status”:“OK”}]}],“status”:“OK”
}

I can then retrieve the result.rows; My problem is when I try to go any further for some reason.

I am specifically trying to retrieve the distance value, I need to determine the closest store to whatever location a person puts in.

When I try to use result.rows.elements.distance.value; for example, it won’t work. I also tried result.rows[0] and I don’t get just the contents of elements for example.

Got it working, but I don’t get why no other method worked still. I had to do the following:-

let dist = row[0].elements;

and then get the next nested value by doing

let nextdist = dist[0].distance;

etc, I don’t know why I can’t go down the chain though, with result.rows.elements.distance; etc

Hi,

You can go down the chain, but you need to keep in mind that some of the “links” in the chain are arrays and must be accessed as such.

Something like this might work. I say might since I haven’t tested it, but hopefully you get the point.

result.rows[0].elements[0].distance.value;

Good luck,

Yisrael

That worked, thanks heaps. Makes complete sense, I must have been blind to the fact that rows and elements were arrays. Thank you heaps!