Search code examples
javascriptpostmanpostman-testcase

Postman Validate key-value pair corresponding to another key-value pair


The Response body is like:

[
    [
        {   
          "id": "1",
          "status": false
        },
        {           
            "id": "1",
            "status": false
        }
    ],
    [
        {           
            "id": "2",
            "status": true
        }
    ]
]

I have to validate if id =1, status should be false, Test gets passed

I tried this:

var jsonData = pm.response.json();

for(var i=0; i<jsonData.length; i++)
 {
   if (jsonData[i][i].Id==="1")
    {

    if (jsonData[i][i].status=== false)
      {

        pm.test("Holiday Update is Sucssesful");
        }   

​ } } But this one is getting invalid token error


Solution

  • Postman test script has support for chai assertions.

    var jsonData = pm.response.json();
    
    for (subArray of jsonData){
        for (subSubArray of subArray){
            if (subSubArray.id == 1){
                pm.test("id with 1 has status false", ()=>{
                    pm.expect(subSubArray.status).to.eql(false)
                })
            }
        }
    
    }