I collected data from the response and pushed it into array in 'Tests'. console.log shows that I received array:
Then I saved it into environment variable to use in the next call. But value in the request was a string, so only the first value was running.
How can I set normal array?
Response from I collected data:
{
"sent": 0,
"remaining": 1000000,
"gifts": [
{
"id": 43468,
"amount": 50000,
"can_gift_back": true
},
{
"id": 43469,
"amount": 50000,
"can_gift_back": true
}
]
}
My code in the "Tests" tab:
let jsonData = pm.response.json();
let gifts = jsonData.gifts;
//calculate array length
function objectLength(obj) {
var result = 0;
for(var prop in obj) {
if (obj.hasOwnProperty(prop)) {
result++;
}
}
return result;
}
let arrayLength = objectLength(gifts);
//push response data to the array
var giftsArray = [];
for (var i = 0; i < arrayLength; i++) {
var giftsIDs = gifts[i].id;
giftsArray.push(giftsIDs);
}
pm.environment.set("giftsToCollect", giftsArray);
UPD:
- After using code from the answer in different ways I received such issue.
Point 1 from the picture describes the way of behavior when stringify is used
Point 2 describes behavior when stringify is not used
2. Example of request JSON with manually inputed ids



JSON.stringify(giftsArray)when saving it to the environment variable?{{var_name}}syntax.gift_ids=[5578,5579,5580,5581,5582,5583,5584,5585,5586,5587](it's captured request body from Fiddler) instead of every value at different level like at the screen from the my answer. Actually, your code didn't help me... and thanks for a short way to collect IDs :)