Use environment var in external iteration json data

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.
[EDITED] example

Pieces involved: iteratinoData from data.json, Postman app request body, Postman app pre-request script, newman js script

–
Is it possible to use an environment variable to the iteration data in a json file?

For example:
Scenario 1: create a storage object to be used in following scenario -> will return respective objects with an id to be used in next iteration/scenario
Scenario 2: create a template with a storageid -> use group from env variable in the pre-request script of “create template” request. the request body would be a variable {{newTemplate}} from iteration data

run collection that runs scenario 1, then scenario 2

EXAMPLE:

Postman Request Body:
{{newTemplate}}

Postman Pre-Request Script:

// check if any new templates are left to create 
let newTemplates = pm.variables.get('newTemplates');

// start of scenarios, so get new templates to create
if (!newTemplates) {
    newTemplates = pm.iterationData.get("templates_data");
}

// set templates storage pool id in environment variables, using variable previously set
let templateStoragePool = JSON.parse(pm.variables.get('storagePool'));
pm.environment.set('templateStorageId', templateStoragePool.id);

// get first template from data.json templates_data object
let currentTemplate = newTemplates.shift();

// expect this to be used in Request Body with `{{templateStoragePool}}`
pm.environment.set("newTemplate", JSON.stringify(currentTemplate)); 

pm.environment.set("newTemplates", JSON.stringify(newTemplates));

// handle repeating request with next newTemplate if it exists
... 
}

iterationData : data.json

[
  {
    "scenario": "create a template and then a user",
    "routes": [
      "Create a template",
      "Create a user"
    ],
    "templates_data": [
      {
        "name": "template A",
        "storage": [
          {
            "storageId": "{{templateStorageId}}",
          }
        ]
      },
      {
        "name": "template B"
        "storage": [
          {
            "storageId": "{{templateStorageId}}",
          }
        ]
      }
    ]
  }
]

currently my newTemplate object that goes into the request looks like this when I do console.log:

        {
        "name": "template A",
        "storage": [
          {
            "storageId": "{{templateStorageId}}",
          }
        ]
      }

but what I want would be:

        {
        "name": "template A",
        "storage": [
          {
            "storageId": "190340-293849-13894",
          }
        ]
      }

newman script

function testCollection() {
  newman.run({
    collection: <myCollection>),
    iterationData: data.json
  }, function (err) {
    if (err) { throw err; }
    console.log('collection run complete!');
  }); 
}

*this is a very oversimplified example i came up with, in reality my scenarios/routes are much more complex so being able to pass variables this way would be really useful

TIA!

1 Like

Yes this work , in any field that allows calling variables as {{variable name }}

Example: request body

My question is how would I achieve this within my external json iteration data.

When the iteration data is read in, it reads the value as the string "{{region1id}}" instead of as an object and doesn’t get replaced by the env variable for region1id

I was hoping it would apply the region1id environment variable to the value of 'group', in the pre-request script, so when it gets used in the request body, the value is set correctly, instead of in the first newUser example I mentioned above.

Right now I am replacing the value in my pre-request script to make it work, (user.group = pm.environment.get('regionid')), but wanted to know if there’s a way to avoid writing code for this, or what I’m missing if it’s a matter of me doing something incorrectly

pm.environment.set("hi",1)

console.log(pm.variables.replaceIn("this is {{hi}}")) 

you can use pm.variables.replaceIn for script section . Everywhere else it will be replaced automatically

This is because variables are not referred as {{name}} in script sections

I’m not sure this is the same use case. I’m going to edit my original post to make it more clear, if you would look at it again I would appreciate it

[EDIT]
done editing original post

where are you using console.log to print it ? you should chack console and see what was actually send . It should be resolved correctly

My prerequest:

pm.environment.set("hello","123")

pm.environment.set("hi",JSON.stringify({

    "mean": 1710.2180279856818,

    "count": {"mean":"{{hello}}"},

    '5MinuteRate': 34.74630977619571,

    '15MinuteRate': 11.646507524106095

}))

RequestBody

make sure to select raw>json

console: (see whats actually send)

I have a console.info in my pre-request script right before setting the newTemplate env var:

console.info(template to create: ${JSON.stringify(currentTemplate)})

doubled checked and my RequestBody is correctly set to raw>json
I’m running a script to call newman from terminal (see SS - this has the actual data i used so I truncated it a bit):
image

[EDIT]
Ok, now I see that the log statement wouldn’t print it correctly but the request theoretically should work correctly - I will experiment a bit and update

Ok, I printed console.log(request.data); under Tests and I see that the values are set properly!

Thank you!