Concade dynamic variable with pm.response.json() to fetch response value

I see quite a few problems in the code you posted, but I think I get the gist of what you’re trying to do. You want to dynamically assert that a nested property N levels deep equals a value. You can do that with this adjusted script:

const responsebody = pm.response.json();

// Assuming sortField looks like this: contact.address.city.town
const sortField = pm.variables.get("sortField");

// Assuming sortValue looks like this: "Building 1"
const sortValue = pm.variables.get("sortValue");
const fields= sortField.split(".");

// I'm not sure where to start or your requirements around array processing, so we'll do it this way.
let currentField = responseBody.response[0];
for(const field of fields) {
  if(currentField) {
    // Dive deeper into the field nest
    currentField = currentField[field];
  }
}

pm.expect(currentField).to.equal(sortValue);

This doesn’t feel exactly what you’re doing, but I hope it gets you close. I am having a hard time following your code :sweat_smile:

I’m more than happy to answer any questions you might have on this!