Call jsonData in loop by passing parameters

Hello,

I have a problem that I cannot solve, even after several hours of research.

I want to pass a hash table. This must be called in a loop, and a test is performed : I check the body variables. The hash table key corresponds to this variable. The hash table value corresponds to the path to find this variable.

Body :

{
“post code”: “90210”,
“country”: “United States”,
“country abbreviation”: “US”,
“places”: [
{
“place name”: “Beverly Hills”,
“longitude”: “-118.4065”,
“state”: “California”,
“state abbreviation”: “CA”,
“latitude”: “34.0901”
}
]
}

Test :

var jsonData = pm.response.json();
const id = “id”;
const expected = “expected”;

const complex_arr=[
{id:“country”, expected:“country”},
{id:“longitude”, expected:“places[0].longitude”},
{id:“state”, expected:“places[0].state”},
{id:“latitude”, expected:“places[0].latitude”}
]

for(i=0; i<complex_arr.length; i++){
var actualId = complex_arr[i].id;
var actualExpected = complex_arr[i].expected;

pm.test("Check " + actualId, function () {
    pm.expect(pm.variables.get(actualId)).to.equal(jsonData.actualExpected);
}

It is this last line that bothers me (jsonData.actualExpected). Indeed, jsonData is an object and i want to “concatenate” the jsondata with the variable path, which is a string. I tried to use some methods to succeed (ie. JSON.parse, JSON.stringify, concatenation, …) but nothing works.

Is it possible, and if so, can you enlighten me please?

Thanks !