Folder run with json file - How to send complex objects?

Hi there,

I am trying to automate a simple test case for a project.
I have an endpoint that converts a Notion page using its id and some dynamic variables to html for a subsequent request using Gmail API.

I have created a json test file with the following structure:

[
    {
        "email_variables": [
            {
                "key": "",
                "value": ""
            }
        ],
        "notion_id": "",
        "email": "",
        "full_name": "",
        "job_title": "",
        "phone": ""
    }
]

My test aim at veryfing that the function is dynamically applying the correct email signature with a bunch of post script tests on the first request (inside request directly):

/*console.log(pm.request.body.raw);*/

pm.test("Response must be valid and have a body", () => {
    pm.response.to.be.ok;
    pm.response.to.be.json; // Fixed syntax
}); 

let responseData = pm.response.json();
pm.collectionVariables.set("email_object", JSON.stringify(responseData)); //set for the next request

pm.test('signature_exists', () => {
    pm.expect(responseData.body).to.exist; // le champ existe
    pm.expect(responseData.body).to.not.be.empty; // il n'est pas vide
});

[...]

This set up works well when running requests individually but I encounter an issue when using the “Run folder” option.
The email_variables array is not properly mapped for each iteration. I struggle the find a way to either parse or do something with this property but I have been unsuccessful so far.

The request body for each iteration is not valid:

[
    {
        "variables": [object Object
        ],
        "notion_template": "..."
    }
]

How can I convert a complex object and map an array for each request inside the request body?

Thank you in advance

I can replicate that issue when using complex JSON in the data file.

It’s treating the array as an object.

I’m pretty sure that I’ve read somewhere on this forum that its meant to be a flat JSON object for the data file.

What you can do is retrieve the existing body, update it directly and then overwrite the entire body for the request.

Something like the following in a pre-request script…

let body = JSON.parse(pm.request.body.raw);
body[0].variables = pm.iterationData.get("email_variables");
pm.request.body.raw = body;