Home > Blockchain >  Saving response to environment with multiple values
Saving response to environment with multiple values

Time:01-28

I'm trying to save these IDs, but they are not getting saved to my environment variables and I'm seeing the console output as:

undefined
undefined

Json Response

{
    "savedIds": [
        95672,
        95673
    ]
}

Test

for(let i = 0; i< jsonData.savedIds.length; i  ) {
pm.environment.set("savedID"   [i 1],jsonData)
console.log(pm.environment.get('savedIds'   [i 1]));
};

CodePudding user response:

Firstly, your code to get JSON data works well such as written here:

let jsonData = {
    "savedIds": [
        95672,
        95673
    ]
};

for(let i = 0; i < jsonData.savedIds.length; i  ) {
  console.log(i   ", "   jsonData.savedIds[i]);
};

The issue come from the key used with Postman. You are using savedID then savedIds. So, you will do something like this:

for(let i = 0; i< jsonData.savedIds.length; i  ) {
  pm.environment.set("savedID"   [i 1], jsonData.savedIds[i])
  console.log(pm.environment.get('savedID'   [i 1]));
};
  •  Tags:  
  • Related