Objects in Arrays passed as a JSON body

I’m sure this is just a syntax issue somewhere on my end, or I’m not understanding the console output. I have this script that will read through an array of objects:

let mzs = pm.collectionVariables.get(“mzs”);

if(!mzs || mzs.length == 0) {
mzs =[{“id”: “test1”, “email”: “[email protected]”,“firstName”: “test”,“lastName”: “test1”,“passwordClearText”: null,“groups”: [“TestGroup”]},{“id”: “test2”,“email”: “[email protected]”, “firstName”: “test”, “lastName”: “test2”, “passwordClearText”: null, “groups”: [“TestGroup”]}];
}
let currentmz = mzs.shift();
console.log(currentmz)

However, with this I get a syntax error if I try to call just the currentmz variable as the body.

Where I am a little confused is the console.log for currentmz shows the proper json object:

But in the response body I am just seeing the raw input of the variable:

Is that expected, or should I be seeing the full value for the variable as the request body? For more context, if I take that json object and put that as the body, this call works fine:

So, again, pretty sure I’m just missing something really small here, and any help is appreciated. The end goal will be to dump this into a collection and iterate through multiple objects, but need to get 1 working before I get to the post script stuff.

Thank you!!

This is a scope issue. That local variable will not be available to the body tab.

You will need to store the current value (currentmz) into a collection or environment variable and it will then be available.

That check should also be updated slightly.

if(!mzs || mzs.length == 0) {

This will cause a fatal exception if mzs does not exist. You will get a Postman error and the request won’t run at all.

The following won’t cause a hard exception.

if (typeof mzs === 'undefined' || mzs.length == 0) {

If you want to use a variable in the URL or body, then you need to save the data to a collection or environment variable.

However, you don’t really need to save this as a variable, as you can overwrite the current body using pm.request.body.raw.

Just set the body as RAW\JSON but don’t add anything to the body. (Anything you do will be overwritten anyway).

All you need to control your loop is the following.

if (typeof array === 'undefined' || array.length == 0) {
    array = pm.collectionVariables.get("mzs");
}

let currentMzs = array.shift();
pm.request.body.raw = currentMzs;

if (array.length > 0) {  
    currentRequest = pm.info.requestName;
    pm.execution.setNextRequest(currentRequest);
} 

The array is set as a global variable and will be available throughout the collection run.

We use array.shift() to get the first object in that array, and then set that object as the body for the request.

You can control the loop within the pre-request script. No need for extra code in the post-response tab. Remember that setNextRequest() just sets the next request that will run after the current request and all of the code in the scripting tabs has completed.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.