Refining an array of objects in pre-request scripts

My question:

Hi everybody,
I need your help.

By calling a GET API through Postman, I get an array of objects like this (it’s just an anonymized example, the actual objects are much longer):

[
{
“id”: “123456789”,
“name”: “Paul”,
“surname”: “Green”,
“country”: “Germany”
},
{
“id”: “987654321”,
“name”: “Mary”,
“surname”: “Jane”,
“country”: “France”
},
{
“id”: “192837465”,
“name”: “John”,
“surname”: “White”,
“country”: “Spain”
}
]

What I need is to:

  1. store the whole array of objects into a collection variable (say, “wholeResponse”);
  2. create a pre-request script in the subsequent POST call, so that it will pass a refined array of objects (containing only, say, “id” and “country”).

In the next POST call, the request body should look like this:

[
{
“id”: “123456789”,
“country”: “Germany”
},
{
“id”: “987654321”,
“country”: “France”
},
{
“id”: “192837465”,
“country”: “Spain”
}
]

I’ve already tried:

I’ve managed to accomplish step 1, i.e. to tailor a test script (concurrent with the GET) which stores the whole array of objects into a variable:

let responseData = pm.response.json();
pm.collectionVariables.set(‘wholeResponse’,JSON.stringify(responseData));

But after doing this, I have no idea how to refine the array I have stored for subsequent use.

Any help from your side would be highly appreciated, thank you in advance! :slight_smile:

Another viable option could be to store into the variable, from the very beginning (when I run the GET), just the data I want to reuse.

I found here a test script filtering the subset from JSON response (nested array), and i adapted it as follows:

var resp = pm.response.json();
subset = [];
pm.collectionVariables.set(“refinedData”, subset);
for (i=0;i<resp.length;i++)
{
var subsetData = _.pick(resp[i], [‘id’,‘country’]);
console.log(subsetData);

}

The problem is that, although I do find the results filtered in the console log, nothing is being saved into the variable “refinedData”.
Why? How could I have the script store the filtered data into my variable?

Thank you for any suggestion you can provide.

When do we receive the below error message:

“Message Details Id | AssertionError: the given combination of arguments (undefined and string) is invalid for this assertion. You can use an array, a map, an object, a set, a string, or a weakset instead of a string”

This is the response in the console, we can view multiple arrays
“data”: [
{
“secure_status”: “0”,
“subject”: “Hello admin”,
“body”: “hello testing sent messages at Patient Portal 1”,
“message_time”: “2023-01-17 03:36:05”,
“message_id”: “4279482107396358256”,
“thread_id”: “4279482107396358256”,
“message_details_id”: “4279482107396358257”,
“user_id”: “1”
},
{
“secure_status”: “0”,
“subject”: “Re:Hello admin”,
“body”: “hello test msg 200”,
“message_time”: “2023-01-17 05:25:49”,
“message_id”: “4279482107396358356”,
“thread_id”: “4279482107396358353”,
“message_details_id”: “4279482107396358357”,
“user_id”: “1”
},
{
“secure_status”: “0”,
“subject”: “Message from Clinician 1”,
“body”: “Do we have any appointment today?”,
“message_time”: “2023-01-17 06:07:42”,
“message_id”: “4279482107396358369”,
“thread_id”: “4279482107396358363”,
“message_details_id”: “4279482107396358370”,
“user_id”: “1”
}

This is the test case used for testing.

const response = pm.response.json();

console.log(response.data[0][“message_details_id”]);
SENDMessage=pm.collectionVariables.get(‘cache-sendMsg’);

pm.test(“Message Details Id”,()=>{
pm.expect(response.data[“message_details_id”]).to.include(SENDMessage)

});

ISSUE: I am not able to catch the specific response of all the responses of the console

Can someone help?

@vaishalialle

What pm.response.json() does is parse the response body to a JavaScript object.

JavaScript objects, can contain other JavaScript objects, and an array is a specific type of JavaScript object. With complex JSON, you can have objects within objects and arrays within arrays. It’s important to understand what is an array, and what is a object, as certain JavaScript functions like map and find only works against arrays.

In your example, you have a single array called data, with multiple JavaScript objects within that array.

Are you just trying to assert that your collection variable exists in the message_details_id somewhere in the response. It doesn’t matter which record?

If you want all of the message_details_ID, then please review the following example(s). A singular request, and one wrapped in a loop.

response = pm.response.json()

let messageDetails = response.data.map(data => data.message_details_id);
console.log(messageDetails);

pm.test("Message Details includes ID 4279482107396358370", () => {
    pm.expect(messageDetails).to.be.an('array');
    pm.expect(messageDetails).to.include('4279482107396358370');
});

const IDs = ["4279482107396358257", "4279482107396358357", "4279482107396358370"]

IDs.forEach(id => 
    pm.test(`Message Details includes ID ${id}`, () => {
        pm.expect(messageDetails).to.include(id);
    })
);

image