How to dynamically format assertion in Postman

var responsebody = pm.response.json()

var new1="[contact]"
var new2= "[address]"
var new 3 ="[city]"
int i =0;
var data="responsebody[i]"
var newdata = data+new1+new2+new3

This is not working:

pm.expect(newdata).to.equals("New York");

Issue is showing Assertion response[i][new1][new2][new3] is not equal to (“New York”)

But this is working -

pm.expect(response[i].[new1].[new2].[new3]).to.equals("New York");

Can we change this string value?

"response[i][new1][new2][new3]"  into object and pass to pm.expect(); so that extract response value

need to create this string dynamically .

Can you access these nested properties like I mentioned in your other post?

Will that work for you?

You need to read the other thread, which should explain why you can’t target elements like this.

Concade dynamic variable with pm.response.json() to fetch response value - Ask the Experts and Postman Tips - Postman Community

You cannot target the element using a string using plain old JavaScript.

You can however, use Lodash.

1 Like

Assertion error undefine for
pm.expect(currentField).to.equal(sortValue)