How to pass an array to the body as json in postman

Hello Everyone,
I want to know how to pass an array to the response body?
I am saving an array as an environmental variable as a string, but I want to pass that array to the response body as an array not as string.
Please anyone help me with this.
This is how I save the variable
const recordsArr = JSON.parse(responseBody).records;
pm.environment.set(“recordsArr”, JSON.stringify(recordsArr));
And I want to pass like this.

  "dynamic_template_data": {
    "records": {{recordsArr}} 
  }

recordsArr is an array of object
when I try to access the value inside the array inside the body, it doesn’t return anything, it’s not able to access nested property inside the stringfy array

This saves the object as a string.

If you want to save it as a JSON object just modify your script to be:

const recordsArr = JSON.parse(responseBody).records;
pm.environment.set('recordsArr', recordsArr);

Thanks for your response
is it ok to directly save the object without converting to string?

It depends on the use case. This one might work, honestly I’m not sure. What I’ve done in the past is set the entire body to a stringified variable and used that in the request.

const recordsArr = JSON.parse(responseBody).records;
const body = {
  dynamic_template_data: {
    records: recordsArr
  }
};

pm.environment.set('body', JSON.stringify(body));

Then in your request body, just drop in the body variable.

image

2 Likes

This is not working for me because I have more text inside the body and body is not an object in my case
Edit: I made it working by manupulating the body object