To assert that the array in the response body contains a string value in all its elements

let jsonData = pm.response.json()

pm.test('The `srNo` in each record' , () => {
    _.each(jsonData.srNo, (item) => {
        pm.expect(item.srNo)
    })
})

//This is what I Tried not going well
//Thanks for the Reply

There are a few ways that you could do this - Sticking to the direction that you were heading in, this could work:

let jsonData = pm.response.json();

pm.test('The `srNo` property is in each record' , () => {
    _.each(jsonData, (item) => {
        pm.expect(item.person, 'Does not have the property').to.have.property('srNo');
    });
});

Hey,
Thanks for the suggestion, It’s working fine now.
again Thank you

1 Like

I am new to Postman. I want to check Id=3, I have tried like below:

pm.test('The `Id` contains 3', () => {
    _.each(jsonData.Fee, (item) => {
        pm.expect(item.Id).to.include(1)
    })
})

Actually, it is giving an assertion error. expected 2 to include 1

Hey @arunece09

I’m not sure what it is you’re trying to assert against, you might need to share the response body too.

The code is looping through the data so it will check each object and if all the object Id properties are not 1 then it would fail like you can see.

1 Like

This is from another script, Hope you can make this together.

pm.test("The response has all properties.", () => 
{
    pm.expect(jsonData).to.be.an("array");
    pm.expect(jsonData[0].channel.id).to.be.a("string");
    pm.expect(jsonData[0].postList).to.be.an("array");
});

Hey @danny-dainton

I want to ask you about this request

pm.test('The "Number_1" contains ', () => {
    _.each(jsonData.Fee, (item) => {
        pm.expect(item.Number_1).to.include('+33 ')
    })
})

But it didn’t work unfortunately when i searched throw 2 arrays like this i want to get except for the “Number_1” and “Number_2”

{
   "Client_Data":{
      "User":{
         "Name":"Juan",
         "Surname":"Pablo",
         "Birthday":"12.07.1991",
         "Contact":{
            "Telefon":[
               {
                  "Number_1":"+33 6084986932",
                  "Number_2":"+33 4596127896"
               }
            ]
         }
      }
   }
}

Thank you so much for your quick response. Here is my response body.

{
   "data":[
      {
         "id":1,
         "email":"george.bluth@reqres.in",
         "first_name":"George",
         "last_name":"Bluth",
         "avatar":"https://reqres.in/img/faces/1-image.jpg"
      },
      {
         "id":2,
         "email":"janet.weaver@reqres.in",
         "first_name":"Janet",
         "last_name":"Weaver",
         "avatar":"https://reqres.in/img/faces/2-image.jpg"
      }
   ]
}

Below is my Tests, where I need to check email is george.bluth@reqres.in present in the response body.

let JsonData= pm.response.json();

pm.test("Verify email", () => {
    _.each(JsonData.data, (item) => {
        pm.expect(item.email).to.include('george.bluth@reqres.in')
    })
})

it is giving the Assertion error: expected janet.weaver@reqres.in to include george.bluth@reqres.in

Hey @mw49

As you’re copying the code from a completely different response body, all the references you’re using are not correct.

You could use something like this to check the values of the items in that single object, with the array. It’s using .match to check the value.

This is a good site to get more understanding of how regex works - regex101: build, test, and debug regex

As you’re looking at all the values in that object, you would need to get them before you can loop through them, that’s why i’m using _.values to get them from the object. You can see what values are captured by uncommenting the console.log statement.

let jsonData = pm.response.json();

pm.test('The numbers contain +33 ', () => {
    let phoneNumbers = _.values(jsonData['Client_Data'].User.Contact.Telefon[0]);
    // console.log(phoneNumbers);
    _.each(phoneNumbers, (number) => {
        pm.expect(number).to.match(/\+(33)\s\d+/);
    });
});
1 Like

As you’re using a loop, it’s looking at each of the email properties and checking if it includes that value. As the second object doesn’t include that, it’s failing.

This would target that specific value in the first object:

pm.test("Verify email", () => {
   pm.expect(jsonData.data[0].email).to.eql('george.bluth@reqres.in')
})

You could use something like this to check them all and give a true/false if it’s present:

pm.test("Verify email", () => {
   pm.expect(jsonData.data.some(a => a.email === 'george.bluth@reqres.in'), 'Email not in array').to.be.true;
})
2 Likes

I can see that many people are asking follow up questions on this thread that don’t relate to the original question.

I’m going to close this topic and ask that you create a new topic that contains all the details for your particular question, in your own context.

It will save others on the thread receiving notifications for non-related questions. :smiley: