AKADO
AKADO

Reputation: 481

POSTMAN: How to Check in Json response Array if a key:value exist

I want to check in my JSON response Array if a key:value exists. and I want if the key:value is found get out from the loop and get the test result. Can you please support me how to do it?

e.g. I have the following response:

[
    {
        "persID": "personID_1",
        "vip": false,
        "account": {
            "contactName": "value1",
        },
        "premium": {
            "Name": "value2",
            "Director": "value3",
            "company": "value7",
            "homePage": "value6",
            "address": {
                "country": "value8",
                "city": "value9"
            },
            "photo": value10
        }
    },
    {
        "persID": "personID_2",
        "vip": false,
        "account": {
            "contactName": "value11",
        },
        "premium": {
            "Name": "value12",
            "Director": "value13",
            "company": "value17",
            "homePage": "value16",
            "address": {
                "country": "value18",
                "city": "value19"
            },
            "photo": value110
        }
    },
    .....
    .....// dynamic response can be "n" elements!!!!
    ]

and I want to check if in this array a key value with ("persID": "personID_3") exist. (e.g. for persID_3, should the result be failed, and persID_2 passed)

I have tried the following but no result:

var jsonArray = pm.response.json();
var persID = "persID_3";
pm.test("2. tets check if persdID exist in array", function () {
 var i=0;
 for(i; i<jsonArray.length;i++){

                pm.expect(jsonArray).to.have.property(jsonArray[i].persID, persID);
                // pm.expect(jsonArray[i]).to.have.property(jsonArray[i].persID, persID);

 }
});

Also tried with var jsonArray = JSON.parse(responseBody); and pm.expect(jsonArray[i]).to.have.property(jsonArray[i].persID, persID);

but no result

Thanks for any Support.

Upvotes: 9

Views: 25071

Answers (3)

AKADO
AKADO

Reputation: 481

Thanks, Danny & Sivcan, with your support I could find out what I need, maybe is not nice coding but it works. I could not get with your function (from Danny) how to get out from the loop. so with this solution was working for me:

// Check if the netry with Key:value persID:regDB_persID exist in Array
var jsonArray = pm.response.json();
var var_regDB_persID = pm.environment.get("regDB_persID");  // environment var from registration

var flag = false;
 // var i=0;
    for(var i=0; i<jsonArray.length;i++){
        _.each(pm.response.json(), (arrItem) => {
            if (arrItem.persID === (var_regDB_persID)){
                // throw new Error(`Array contains ${arrItem.persID}`)
            console.log(flag = true);
            }
        });
    }

    pm.test('2. Get Info for all accepted User from Admin - Matches personID property value', function(){
        if (flag === true) {                        
            return;}  // persID was in response with value   
            else {
             throw new Error("Array Does NOT contains" + var_regDB_persID)}
    });

in this case, it will pass the case if the property with value exists.

Upvotes: 1

Danny Dainton
Danny Dainton

Reputation: 25881

You could use Lodash to loop through the array and check for the value in the response:

pm.test('Matches personID property value', () => {
    _.each(pm.response.json(), (arrItem) => {
        if (arrItem.persID === 'personID_2') {
            throw new Error(`Array contains ${arrItem.persID}`)
        }
    })
});

This should fail the test if the personID_2 property exists but this value could be anything you like. I used your response data and created a quick API route to show you this working.

Postman

I added the value in to the Error message so that it is more descriptive on the test output.

Upvotes: 6

Sivcan Singh
Sivcan Singh

Reputation: 1892

Postman uses an extended implementation of the chai library. https://github.com/postmanlabs/chai-postman

You can use something like this to check

I am hardcoding the response object. You can parse it from your API response as you are already doing it using pm.response...

let resp = [{
    "persID": "personID_1",
    "vip": false,
    "account": {
        "contactName": "value1",
    },
    "premium": {
        "Name": "value2",
        "Director": "value3",
        "company": "value7",
        "homePage": "value6",
        "address": {
            "country": "value8",
            "city": "value9"
        },
        "photo": "value10"
    }
}, {
    "persID": "personID_2",
    "vip": false,
    "account": {
        "contactName": "value11",
    },
    "premium": {
        "Name": "value12",
        "Director": "value13",
        "company": "value17",
        "homePage": "value16",
        "address": {
            "country": "value18",
            "city": "value19"
        },
        "photo": "value110"
    }
}];

pm.test('Finding person ID', function() {
    let personId = "personID_1";

    _.forEach(resp, (respObj, index) => {
        if (respObj.persID === personId) { // or do the vice-versa
            console.log('Found', respObj);
            throw new Error('Found it!'); // This will make the test fail
        }
    });
    // If not found then the test will pass... 
});

Upvotes: 1

Related Questions