How to dynamically pass array of values for multiple keys?

Body contains numerous keys - but I am only testing two of the keys.
Key1 and Key2.

Key1 can be [a,b,c,d].
Key2 can be {a:[1,2,3], b:[4,5,6], c:[7,8,9], d:[10,11,12]}

I had no issues passing values for each test when one key is involved.

In my pre-request tab:

let keyObj = pm.collectionVariables.get("keyObj");
keyObj = {'a':[1,2,3], 'b':[4,5,6], 'c':[7,8,9], 'd':[10,11,12]
}

for (let i in keyObj ){
    let key1 = i;
    pm.collectionVariables.set("key1",key1);
    let keyVals = keyObj[key1];

    for (let j=0; j < keyVals.length; j++){
        let tempVal = keyVals[j];
        console.log(key1,tempVal);
        pm.collectionVariables.set("key2",tempVal);
        //i want the request to be sent here!
        
        
    }

    delete keyObj[i];
}

And in my Test tab:

let keyObj = pm.collectionVariables.get("keyObj");
if (Object.keys(keyObj).length == 0){
postman.setNextRequest(null);}
else {
postman.setNextRequest(pm.info.requestName)};

//assertions below

The problem is it’s only sending the request ONCE for key1 = 'd' and key2 = 12
Is there any way to kick off the request as soon as I set the variables? Please feel free to make suggestions on how I’m looping through the data.

Thank you.

Are you in control of how key2\keyObj is formatted?

Can we change the format so its a proper array with each combination as a separate object?

[{"a":1},{"a":2},{"a":3},{"b":4},{"b":5},{"b":6},{"c":7},{"c":8},{"c":9},{"d":10},{"d":11},{"d":12}]

This allows you to use an function available to arrays called array.shift().

This will retrieve the current value\object from the array, and will also delete the value from the array.

You will then use this in conjunction with setNextRequest to control your loop.

You can control all of this in a pre-request script. Something like…

if (typeof array === 'undefined' || array.length == 0) {
    array = [{"a":1},{"a":2},{"a":3},{"b":4},{"b":5},{"b":6},{"c":7},{"c":8},{"c":9},{"d":10},{"d":11},{"d":12}]; 
}

let currentObj = array.shift();

pm.collectionVariables.set("key1", (Object.keys(currentObj)));
pm.collectionVariables.set("key2", currentObj[pm.collectionVariables.get("key1")]);

if (array.length > 0) {  
    currentRequest = pm.info.requestName;
    postman.setNextRequest(currentRequest);
} else {
    postman.setNextRequest(null)
}

I’m just testing this against Postman Echo with query parameters.

image

1 Like

Awesome that worked! I was thinking of modifying the data structure.

How would you suggest reformatting key2 to the new format within the pre request tab? Lets say the array is the format I want to use to set the collection variables. In the following block:

if (typeof array === 'undefined' || array.length == 0) {
    array = [{"a":1},{"a":2},{"a":3},{"b":4},{"b":5},{"b":6},{"c":7},{"c":8},{"c":9},{"d":10},{"d":11},{"d":12}]; 
}

Do I just set array = key2, assuming key2 has been reformatted?

If key2 is already in the correct format, then you would use that instead of the array variable.

I just use that conditional statement to reset the array, just in case you want to run this more than once. If you are reading the initial array from a collection variable or somewhere else, then you would need to cater for this.

In my example, you only need to set collection variables for the current values that you want to use in the request, so they can be targeted in the URL or Params\Body tab.

image

Local variables can’t be used in those tabs without first setting the values as collection or environment variables.

Local variables do however persist through a collection run, which is why I don’t have to keep resaving the array each time. As you might see in other examples that utilize the array.shift() method.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.