Writing runner payloads dynamically to an array?

Hello, quick question: Can I programmatically loop over data in a JSON file, taking iteration data for the first X number of entries, and combine them into a single request?

Scenario:

Say I have 100 JSON objects to send to an API, so I use a Postman Runner to split them into 1 group of 100, or 10 groups of 10.

Some example that may not work since I wrote it in this post:

pm.iterationData.set(‘payload’, for (let i = 0; i < 100; i++)
{let currentObject = jsonArray[i];
for (let key in currentObject) {
let value = currentObject[key];
${key}: ${value};
} }

Or would it be better to format my JSON into the desired payload, then use the pre-request script to iterate over payloads in parent array? This would then be an array of objects, with each object containing an array of the exact key-values I want to send in the request.

Each “payload” to iterate over would look like this, in a big array where:
[
{
payload: {
“products”: [{
“productId”: “1234”,
“quantity”: 1
},
{
“productId”: “123”,
“quantity”: 12
},
{
“productId”: “1000”,
“quantity”: 5
}],
“location”: “WIN”
},
“payload”: {

}
]

pm.iterationData.set(‘payload’, payload)
curl –request blah blah
–data ‘{{payload}}’

I would have to code further to split the one large file into the array of objects with arrays.

TMI background data: My company has to modify Salesforce data for 20,000 entities. The API I created to do this accepts batchable requests. I wanted to put it in Postman so that I could share it with my team in case of unavailability for me, the person in charge.

TIA

Not sure what you are doing there.

pm.iterationData.set isn’t a valid method as pm.iterationData is read-only. It’s designed to retrieve data for the current iteration, not to modify it.

The collection runner isn’t that advanced. It either needs an CSV file or JSON array.

It will run one iteration for each line of CSV, or for each object in the JSON array.

It doesn’t support complex JSON very well.

You cannot do this.

As it will be unable to read that object properly. Showing [Object Object] if you console log it.

What you can do is set the request to raw\JSON and leave the body blank.

You can then use a small pre-script script to overwrite the body from the iteration data.

pm.request.body.raw = pm.iterationData.get("payload");

Based on the following data file.

[
    {
        "payload": {
            "products": [
                {
                    "productId": "0001",
                    "quantity": 1
                },
                {
                    "productId": "0002",
                    "quantity": 12
                },
                {
                    "productId": "0003",
                    "quantity": 5
                }
            ],
            "location": "WIN"
        }
    },
    {
        "payload": {
            "products": [
                {
                    "productId": "0001",
                    "quantity": 1
                },
                {
                    "productId": "0002",
                    "quantity": 12
                }
            ],
            "location": "WIN"
        }
    },
    {
        "payload": {
            "products": [
                {
                    "productId": "0001",
                    "quantity": 1
                }
            ],
            "location": "WIN"
        }
    }
]

You can see this run three times with the varying number of products in the payload\body.

1 Like