Name of Matrix Returned

Hey, I’ve recently used OpenRouteService API for Distance Matrix.

But now that the matrix shows in my output on VSCode, I don’t know how to parse the returned JSON.
By that, I don’t know what is the name I have to put in.

let XMLHttpRequest = require('xhr2');
let request = new XMLHttpRequest();'POST', "");

request.setRequestHeader('Accept', 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8');
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Authorization', 'xyz');

request.onreadystatechange = function () {
  if (this.readyState === 4) {
    console.log('Status:', this.status);
    console.log('Headers:', this.getAllResponseHeaders());
    console.log('Body:', this.responseText);

const body = '{"locations":[[2.3556456565856934,48.787715911865234],[2.3691248893737793,48.79338073730469],[2.3862559,48.7928631]],"id":"matrix","metrics":["distance","duration"]}';


const obj = JSON.parse();

Here is the code.

I’m relatively new to JS btw.

Thanks for your answers.


when you say you don’t know

I’m assuming you mean how to get the actual response body from the sent request to pass to JSON.parse(), right?

If so, looking at the XMLHttpRequest-Documentation, it seems as if request.response would be right.

Best regards


Thanks for your answer !!

It seems to work since there’s no error but I have another problem now.

const body = '{"locations":[[2.3556456565856934,48.787715911865234],[2.3691248893737793,48.79338073730469],[2.3862559,48.7928631]],"id":"matrix","metrics":["distance","duration"]}';


const obj = JSON.parse(request.response);

This returns me this error : durations is not defined.

And when i just type console.log(obj);
It just returns null before anything has happened.
I’m new to JS as I said, but I assume it is because, in some sort of way, console.log occurs before anything else in the code, thus making obj null or durations not defined.

Is there a way to fix that ?
Or is the problem somewher else ?

P.S. : the first lines of the code remained unchanged.

Hi @Guide_Me,

Try JSON.stringify when logging objects to console. Objects are not displayed in console.
or just log request.response directly.

Maybe try out our JavaScript package which handles a lot of the request parts and makes it a bit simpler to call our API via JS.

Best regards

1 Like