Variables: passing array variable into collection variables

I have a request body that I am setting using a Collection variable - in a Pre-Request script. I then am also marking one of the properties on that request body as a variable, like so:

var obj = {
  "header": {
    "initiatorId": "Test, The",
    "responderId": "Just",
    "messageId": "e24caae1-07fd-4785-900c-73c698065c99",
    "messageTimestamp": "2020-03-25T14:38:16.198Z",
    "serviceId": "Test",
  },
  "payload": {
    "serviceControl": {
    },
    "intermediary": {
      "firmFcaRef": "{{firmFcaRef}}"
    },
    "applicants": "{{applicants}}",
      "interestServiced": "{{interestServiced}}",
      "equityReleaseCouncilProductsOnly": true,
    },
     }
};


var obj1= [
      {
        "definition": "First Applicant",
        "title": "Mr",
        "forenames": "John",
        "surname": "Doe",
        "gender": "Male",
        "dateOfBirth": "1948-07-25",
        "ccjBankruptcy": false,
        "healthQuestions": {
          "weightKg": 98,
          "heightCm": 180,
          "cigarettesSmokedCountPerDay": 5,
          "cigarettesSmokedDurationYears": 40,
          "rollingTobaccoSmokedUnitsPerDay": 0,
          "rollingTobaccoSmokedDurationYears": 0,
          "hasBloodPressureRequiringMedication": true,
          "sufferedFromStroke": true,
          "hasAnginaRequiringMedication": false,
          "hasCancerRequiringSurgeryChemotherapyRadioTherapy": true,
          "hasParkinsons": "Yes Without Medication",
          "hasMultipleSclerosis": "No",
          "hasRetiredEarlyDueToIllHealth": true,
          "sufferedFromHeartAttack": false,
          "hasDiabetes": false,
          "hasRespiratoryConditionRequiringMedication": false
        }
      }];
      
pm.variables.set("firmFcaRef", "000000");

pm.variables.set("applicants", JSON.stringify(obj1));
  
pm.environment.set("requestBody", JSON.stringify(obj));

I then have a request, in which I use {{requestBody}} variable as the request body.

My issue:
The applicants variable is populating as a string in the request when it is passed in:

How can I pass in the applicants variable to my requestBody variable without it being a string? If I try and leave off the quotes around the variable, the syntax highlighter won’t permit it:

@frankdoylezw You just need to stringify it in the script and then set it as the variable value.
Refer to this example for environment variables, similarly, you’ve to do it for collection variable.

Or you can refer this: Sharing tips and tricks with others in the Postman community

@singhsivcan thank you, but I think I’m already trying what you suggest?
This is how I’m using stringify when I set both variables (see screenshot):

pm.variables.set(“applicants”, JSON.stringify(obj1));

pm.environment.set(“requestBody”, JSON.stringify(obj))

When I try and use “{{applicants}}” within {{requestBody}} however, it always appears as a string? Apart from it being a string the applicants complex object does appear correctly.

Oh I think I spot the problem, can you make it {{applicants}} instead of "{{applicants}}" (remove the quotes).

When you put the quotes around the variables, it forces the variables to get converted to a string.

So, when I try and set {{applicants}} instead of “{{applicants}}” within the {{requestBody}} the syntax highlighter shows a red cross and the request fails if I try to send it?

Ah, I guess I had misunderstood your question overall and didn’t go through the screenshots well enough previously.

Well, let me tell you that you cannot use {{ syntax in the test scripts, this is because the test scripts are run in a JS environment, and your variables won’t be resolved. If you marked it with " then the JS interpreter won’t be throwing the error, and while the request is executed, your variables will be resolved as string, but in the case of the array’s this is why they’re getting converted to having quotes around them.

You’ll have to directly assign it like so:

// This is fine since it's for a string and it'll work
pm.variables.set("firmFcaRef", "000000");

// But for the array, you'd have to do this instead of setting the array to the local variable:
obj.payload.applicants = obj1;

// Now stringify the entire requestBody.
pm.environment.set("requestBody", JSON.stringify(obj));

I hope this helps.
I am guessing you were extracting out the applicants due to some specific reasons, but I am sure you can loop over the data and still build the requestBody data.