Can I append some data in environment variables

I have a response output of a API call:

{
    "orgName": null,
    "roleNames": [
        "TenantDashboardOperator",
        "TenantOperator",
        "TenantSecurityAdmin",
        "TenantSuperAdmin"
    ]
}

When I add the output to a variable, using test script like this:

postman.setEnvironmentVariable("TenantRoles",jsonRBData.roleNames);

The value of TenantRoles variable gets set to the output above, but without the Double Quotes, since it becomes like an array.

Is it possible to create another variable, that has a value as the output of the roleNames, but with all quotes?

For example, for the above output, the value needs to be " “TenantDashboardOperator”, “TenantOperator”, “TenantSecurityAdmin”, “TenantSuperAdmin”. So essentially, I want to append the Double quotes and the comma to create a new variable.

The reason I am trying to do that is that the length of the array variable can be dynamic and based on the output, I need to pass this to another request like below:

{
   "versanms.sdwan-org-workflow":{
       "cpeDeploymentType":"SDWAN",
       "globalId": {
            orgID
         }
      },
      "ikeAuthType":"{{ikeAuthType}}",
      "orgName":"{{newOrgName}}",
      "sharedControlPlane":false,
      "supportedRoles":[
          "TenantDashboardOperator",
          "TenantOperator",
          "TenantSecurityAdmin",
          "TenantSuperAdmin"
        ]
}

As you can see above, I need quotes for each of the values to be passed in the request for it to pass.

Thanks,
Suraj

Hey @surajc

You would want to set the environment variable like this:

postman.setEnvironmentVariable("TenantRoles", JSON.stringify(jsonRBData.roleNames))

Or like this using the newer syntax:

pm.environment.set("TenantRoles", JSON.stringify(jsonRBData.roleNames))

To use that in a Request Body, you can use {{TenantRoles}}, without wrapping it in quotes, to add the dynamic array to your request.

1 Like

Ok, thanks @danny-dainton. Will try this out.

Thanks much, @danny-dainton. This works out ok for me.

1 Like