How to iterate through and access objects in an array without using hardcoded values?

So i am not expert here but want to get away from writing tests that use [1] when trying to access data in an array. I am aware i need to probably create some parameters and loop through the array first and then store the data i need and then process it in my test, this is just too advanced for my current level of postman knowledge so i need some help and tips here about how to do this best.

If this is answered in a previous thread, please just link this in the reply.

This is my test code:

let responseJson = pm.response.json();
pm.test(“INTERVALL1,INTERVALL2 finns”, function () {
pm.expect(responseJson.Owners[0].extentOfControl.code).to.eql(“INTERVALL1”);
pm.expect(responseJson.Owners[1].extentOfControl.code).to.eql(“INTERVALL2”);
pm.expect(responseJson.Owners[0].controlTypes[1].text).to.eql(“test text2”);
});

and this is the json data response i am working with:

How does the assertion need to work?

The owners in your example response is an array. Array’s start at zero.

So is the assertion that owners[0] should eql INTERVALL1, and owners[1] should eql INTERVALL2 and so forth?

It’s easy enough to iterate through the response and embed a test that gets run once for each iteration\owner.

If you can include your example response using the preformatted text option in the editor instead of a screenshot and I can probably provide you with a working example.

.extentOfControl.code is not in any sequence and can have data like INTERVAL1, INTERVAL3, INTERVAL2 and so on so i cannot predict this to come in any order. The assertions i am looking to write are to confirm that in my response i getting values like A30 & INTERVAL2 somewhere in the array, i am not interested in where they exist, just if they exist.

Not sure why the format is flat, i have the response in VS as the original response is from production and i cannot publish this here.

“Owners”: [
{
“firstNames”: [
“Mike”,
],
“lastName”: “Smith”,
“citizenship”: {
“code”: “SE”,
“text”: “Sverige”
},
“extentOfControl”: {
“code”: “INTERVALL1”,
“text”: “25 %”
},
“controlTypes”: [
{
“code”: “A10”,
“text”: “test text1.”
},
{
“code”: “A30”,
“text”: “test text2.”
},
]
},
{
“firstNames”: [
“Colin”,
],
“lastName”: “Jones”,
“citizenship”: {
“code”: “SE”,
“text”: “Sverige”
},
“extentOfControl”: {
“code”: “INTERVALL2”,
“text”: “50 %”
},
“controlTypes”: [
{
“code”: “A10”,
“text”: “test text3.”
}
]
},
]

In your example, Owners is an array.

Within each owner is several objects and some of those have their own arrays.

Therefore which array do you want to assert against?

Do you just want to check if A30 and INTERVAL2 exist for any owner? (in their respective keys).

There is a preformatted text option in this editor. Otherwise it will all align to the left.

If you are just looking for the value, then this should work.

response = pm.response.json().data

//http://techslides.com/how-to-parse-and-search-json-in-javascript
//return an array of objects according to key, value, or key and value matching
function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));    
        } else 
        //if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
        if (i == key && obj[i] == val || i == key && val == '') { //
            objects.push(obj);
        } else if (obj[i] == val && key == ''){
            //only add if the object is not already in the array
            if (objects.lastIndexOf(obj) == -1){
                objects.push(obj);
            }
        }
    }
    return objects;
}

pm.test("value - code includes INTERVALL2", () => {
    let search = (Object.values(getObjects(response,"", "INTERVALL2")[0]));
    pm.expect(search).to.include("INTERVALL2");
});