Looping through array variable

I am trying to loop through and array for each iteration.

I have below array variable in my environment host variable section:
UserId = “Margin_User”,“QFC_LOB_User”,“QFC_User”

In my Test code, i am retrieving with below:
let Users = pm.environment.get(‘UserId’);

but below length gives me 39 total length instead of 3.

This is causing issue with reading the variables:

can someone help me on this?


Hey sachin,

Welcome to posman community.
The issue is when you store something in environment or from test script it is stored as a string that’s the reason you get the length as 39.

There are 2 ways to handle this

  1. Use the split function to store it in a array like this
let Users = pm.environment.get(‘UserId’);
let UsersArr = Users.split(",");

After this you can access values under UsersArr as UsersArr[0] & the length will be 3.

  1. If you are extracting users from a API call, either chain it to next request or save each value individually rather than a array.

Let me know if this works for you.

Thanks Skandh. This helped. Really appreciate timely solution.