How to use single json file with multiple diff payloads

I have a JSON file containing two payloads for different endpoints: one for POST and the other for PUT requests. When I run the JSON file through the collection runner, I encounter an issue.

If I set the iteration to 2, it results in duplicate data being created in the database. On the other hand, if I set the iteration to 1, only the first request is sent.

This is how my json file looks like:

[
    {
        "endpoint": "/save",
        "method":"POST",
        "A": 2,
        "B": "item 2", 
    },
   {
       "endpoint": "/update",
       "method":"PUT",
       "id": {{id}},
       "A": 2,
       "B": "update1",
   }    
]

You current have an array with two records in it.

Therefore the collection runner will have two iterations.

You need to store the array in an object like following.

{
    "testData": [
        {
            "endpoint": "/save",
            "method": "POST",
            "A": 2,
            "B": "item 2"
        },
        {
            "endpoint": "/update",
            "method": "PUT",
            "id": "123",
            "A": 2,
            "B": "update1"
        }
    ]
}

This will then run once (for the testData object).

You can then access the elements in the array by its array index.

console.log(data.testData[0].endpoint); // save
console.log(data.testData[1].endpoint); // update

Or consider, not having an array at all and structuring the JSON similar to the following.

{
    "post": {
        "endpoint": "/save",
        "A": 2,
        "B": "item 2"
    },
    "put": {
        "endpoint": "/update",
        "id": "123",
        "A": 2,
        "B": "update1"
    }
}
console.log(data.post.endpoint); // save
console.log(data.put.endpoint); // update

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