I have response body as
{
"records": {
"data": [
{
"name": "Anjan",
"contact": {
"address": {
"city": {
"town": "Building 1"
}
}
},
"phone": "+1 466-771-6687"
}
]
}
}
Now i want to verify response body value
My requirement:-
Sometime i need to verify response data with data.contact.address.city
or sometime i need to verify data.contact.phone
.
In future more fields with more dot is required.
I am saving records.data.contact.address.city
and
records.data.contact.phone
in csv file with sortField
header and value in sortValue
header.
My code:
var responsebody= pm.response.json();
var value= pm.variables.get("sortField");
var mainValue=pm.variables.get("sortValue");
var fielddobject = value.split(" .");
var i =0;
var data = "response[i]";
If ( i=0; i< fielddobject.length, i++)
{
var newData ="[fielddobject"+"["+i"]";
var formdata=data+newData
data=JSON.parse(formdata);
}
for(j=0;j<mainValue.length;j++){
pm.expect(data).to.equals(mainValue[j]);
}
Issue: showing undefine with this:
pm.expect(data).to.equals(mainValue[j]);
But when i call below , its working .
pm.expect(response[i].[fielddobject[0]].[fielddobject[1]].[fielddobject[2]]).to.equals(mainValue[j])
I need to generate dynamically below part
response[i].[fielddobject[0]].[fielddobject[1]].[fielddobject[2]]
and then pass into pm.expect()
.
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 
I’m more than happy to answer any questions you might have on this!
var fielddobject = value.split(" ."); is creating array of 4 - ["contact","address","city","town"]
When i am passing manully pm.expect(response[i].[fielddobject[0]].[fielddobject[1]].[fielddobject[2]][fielddobject[3]).to.equals("Building 1"); its working
Or
When i am giving exact value then also working pm.expect(response[i].contact.address.city.town).to.equals(“Building 1”);
When i am creating above value with loop then Assertion error undefined
Based on the fieldobject array length, want to create dynamically this expression response[i].[fielddobject[0]].[fielddobject[1]].[fielddobject[2]][fielddobject[3]) .....[fielddobject[fieldobjectlength-1]and then pass to pm.expect ().
This is giving Assertion error undefined currentField = currentField[field];
Can you show me the console or something? I’m having a hard time following your school of thought. Do you think the code I presented would work for you if we get you past your error? It’s simply diving into the objects given the property names.
I’m not 100% certain what you want to do here, but I suspect the issue is related to having the full path of the JSON key in a variable, and trying to use that variable to target the element in the response.
Using plain old JavaScript, you would have to use bracket notation, but still can’t target the elements using dot or bracket notation properly. You will get undefined when trying to target anything above the top level. Basically, you can only target one level at a time which isn’t that easy with complex JSON.
Bringing it back to basics. Lets just try and get the data key from the response. The following should highlight the issue at hand.
let sortField1 = "records";
let sortField2 = "records.data"
let sortField3 = "records[\"data\"]"
const response = pm.response.json();
console.log(response[sortField1]); // This works
console.log(response[sortField2]); // undefined
console.log(response[sortField3]); // undefined
console.log(response[sortField1]["data"]) // This also works
The workaround is to use the Lodash library which is available within the Postman Sandbox.
This can target the elements using the path in your string variable.
let sortField = "records.data[0].contact.address.city.town";
let value = "Building 1"
const response = pm.response.json();
let _ = require('lodash')
pm.test("key exists and value matches", function () {
pm.expect(_.get(response, sortField)).to.exist.and.to.eql(value);
});

1 Like
You are a lifesaver. Thanks a bunch, @michaelderekjones