Run the collection using array of objects properties as variables

Hello community,
I need some help. Below is attached my code, the problem is accessing to array’s objects properties and using them as variables.

I have generated an array of objects. It is big array and I should run the collection with a different dataset in each run until the size of the array.
First of all You can run the pre-request script to get the array of objects, it’s something like this:

let requestsArray = [{"r_type":"01", "r_target":"1", "u_range":"1"},{"r_type":"02", "r_target":"11", "u_range":"54"},{...}, ...]

I have three properties in my array’s objects - "r_type", "r_target", "u_range".

My request body is x-www-form-urlencoded and looks like this:

<ReportType><![CDATA[{{r_type}}]]></ReportType>
<RequestTarget>{{r_target}}</RequestTarget>
<UsageRange>{{u_range}}</UsageRange>

How can I access to the objects within array and use its properties as variables ?

Pre-request script:

let global1 = ['01', '02', '03', '05']; 
let global2 = [1, 3, 4, 11];
let global3 = {};
global3['1'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
global3['3'] = [31];
global3['4'] = [34];
global3['11'] = [54];

let requestsArray = [];
for (let u = 0; u < global1.length; u++)
{
    for (let j = 0; j < global2.length; j++)
    {
        for (let x = 0; x < global3[global2[j]].length; x++)
        {
            const obj = {};
            obj.r_type = global1[u];
            obj.r_target = global2[j];
            obj.u_range = global3[global2[j]][x];
            requestsArray.push(obj);
        }
    }
}
console.log(requestsArray);
let f = pm.globals.get("f");
if (!f || f.length == 0) {
    f = requestsArray;
}

let current = f.shift();
pm.globals.set("trio", current);
pm.globals.set("f", f);

Test script :

const f = pm.globals.get("f");

if (f && f.length > 0){
    postman.setNextRequest(pm.info.requestName);
} else {
    postman.setNextRequest(null);
}
1 Like

You’re saving the current iteration in a trio variable, but it looks like you’re accessing the variables in the request body with different names. Have you tried adding this to the bottom of your pre-request script?

let current = f.shift();
pm.variables.set('r_type', current.r_type);
pm.variables.set('r_target', current.r_target);
pm.variables.set('u_range', current.u_range);

Also, your script is doing way too much work every time. It can be short circuited at the very beginning by moving your if statement at the very beginning. So your script could become:

let f = pm.globals.get("f");
if (!f || f.length == 0) {
    let global1 = ['01', '02', '03', '05']; 
    let global2 = [1, 3, 4, 11];
    let global3 = {};
    global3['1'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
    global3['3'] = [31];
    global3['4'] = [34];
    global3['11'] = [54];
    
    let requestsArray = [];
    for (let u = 0; u < global1.length; u++)
    {
        for (let j = 0; j < global2.length; j++)
        {
            for (let x = 0; x < global3[global2[j]].length; x++)
            {
                const obj = {};
                obj.r_type = global1[u];
                obj.r_target = global2[j];
                obj.u_range = global3[global2[j]][x];
                requestsArray.push(obj);
            }
        }
    }
    f = requestsArray;
}

const current = f.shift();
pm.globals.set("trio", current);
pm.globals.set("f", f);
pm.variables.set('r_type', current.r_type);
pm.variables.set('r_target', current.r_target);
pm.variables.set('u_range', current.u_range);

Last point of note, I’d highly recommend nenaming your variables to something meaningful. Anyone who is not you is not going to be able to maintain this long term because your variables have no intuitive meaning and you really need to dig into the code to understand what is going on.

Good luck!

1 Like

Thanks @allenheltondev. This is a perfect solution. Array of objects now is working in a way as I wanted. Thanks again and again for your help.
Appreciate it :clap: :clap: :clap:

1 Like

Happy to help! Glad we were able to work through it.

This entire thread saves my day! I was doing pretty much the same, but it was failing. It annoyed me so much, but by coming here I could fix it.

Hi Allen, I have a slightly different requirement. If I have the below array

let data1 = [‘40189109’, ‘3668’];
let data2 = [‘OE’, ‘CE’];

Body is:
[
{
“mfid”: “{{mfid}}”,
“secType”: “{{secType}}”
}
]

I want to pass the array values as Key value pair like -
mfid = 40189109, secType = OE in one shot and get the response.
Similarly in the next set mfid = 3668, secType = CE.

I tried your code but mfid - 40189109 is running for both OE and CE secType and hence not meeting my requirement. Please can you suggest a solution?

Hey @ramofficial1,

Would you mind sharing your code so we can take a look?

@allenheltondev, I have used the same code given by you earlier but still I am sharing the code.

Body

[
    {
        "mfid": "{{r_mfid}}",
        "secType": "{{r_secType}}"
    }
]

Pre-request Script

let f = pm.collectionVariables.get("f");
if (!f || f.length == 0) {
    let data1 = ['40189109', '3668']; 
    let data2 = ['OE', 'CE'];
    
    let requestsArray = [];
    for (let u = 0; u < data1.length; u++)
    {
        for (let j = 0; j < data2.length; j++)
        {
                const obj = {};
                obj.r_mfid = data1[u];
                obj.r_secType = data2[j];
                requestsArray.push(obj);
        }
    }
    f = requestsArray;
}

const current = f.shift();
pm.collectionVariables.set("duo", current);
pm.collectionVariables.set("f", f);
pm.variables.set('r_mfid', current.r_mfid);
pm.variables.set('r_secType', current.r_secType);

Tests

const f = pm.collectionVariables.get("f");

if (f && f.length > 0){
    postman.setNextRequest(pm.info.requestName);
    } else {
    postman.setNextRequest(null);
}