9

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.

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Danny for Answer, but I have tried the following Hope is the same which you mentioned: var jsonArray = pm.response.json(); var i=0; for(i; i<jsonArray.length;i++){ pm.test("2. Get Info for all accepted User from Admin", function () { _.each(jsonArray.persID, function(i) { pm.expect(i).to.have.property('PersID_3') }); }); } But still is always passed. could you give me the correct coding? maybe I have done some mistake.
Let me have a look locally and i'll see if i have something for you - Just to confirm, you want to check for the property and if it's there, you want the test to fail?
Actually, I want to check if a property with Key AND Value X appears in the Array List and if yes the result of test should be passed otherwise failed. (not only the key (if with the property you mean key also the value) FYI the property is always in the array but I want to see if with the special Key is there or not e.g. persID is always in response but want to check for all array element if persID = 3 is there or not. Thanks
Does that code not do what you're asking? It's looping through the whole array, if the property perID has a certain value, personID_2 in this case, it will exit the test and fail. If it doesn't find {"persID": "personID_2} then the test will pass. You may need to revisit your question if this is not what you actually need as it's just getting too confusing to understand what you really need now.
1

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... 
});

Comments

1

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.

1 Comment

Wow that's a mess of unneeded code. I have no idea why you have a for loop and then another loop (_.each()) inside it - These are the exact same thing. What I provided will loop through the whole array and fail if the value is in there. Just replace 'personID_2' with pm.environment.get("regDB_persID"). If you need to see what's happening during the test add console.log(arrItem.persID) under the _.each() statement to see the items that are being checked. Your questions are so confusing to understand and it seems like you change your mind half way through.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.