Skip Property in JSON File If It Contains an Empty String

I have a JSON File that looks something like this. I want to be able to skip the discounts property if it is empty.

[
 {
   "state": "CT",
   "postalCode": "06010",
   "species": "Cat",
   "unit": "years",
   "value": 0,
   "gender": "Male",
   "breed": "Feral",
   "discounts": "",
   "reimbursement": 70,
   "deductible": 250,
   "annualLimit": 5000
 },
 {
   "state": "CT",
   "postalCode": "06010",
   "species": "Cat",
   "unit": "years",
   "value": 0,
   "gender": "Male",
   "breed": "Feral",
   "discounts": "military",
   "reimbursement": 70,
   "deductible": 250,
   "annualLimit": 5000
 }
]

In the file, it shows it as an empty string but the payload doesn’t accept that as a valid enum value.

Payload:

{
    "productCode": [
        "safecoPet"
    ],
    "primaryResidence": {
        "state": "{{state}}",
        "postalCode": "{{postalCode}}"
    },
    "pets": [
        {
            "name": "Charlie",
            "species": "{{species}}",
            "breed": "{{breed}}",
            "age": {
                "unit": "{{unit}}",
                "value": {{value}}
            },
            "gender": "{{gender}}",
            "birthDate": "2018-09-03",
            "altered": true
        }
    ],
    "productOptions": {
        "reimbursement": {{reimbursement}},
        "deductible": {{deductible}},
        "annualLimit": {{annualLimit}},
        "coverageTypesSelected": [
            "Wellness"
        ],
        "discountSelected": [
            "{{discounts}}"
        ]
    }
}

Error response:

{
    "code": 400,
    "message": "Bad request",
    "description": [
        "/productOptions/discountSelected/0  is not a valid enum value"   
}

Any help is much appreciated!

You could do it with javascript in the Pre-request Script section.
The idea is to initialize your payload template and then search for {{varName}}.
If from the iteration data the varName has a value that is a non empty string, then replace it.
If not, then execute a function to manipulate the json object of the payload.
Here In the case of an empty discounts variable, I decided to replace it by an empty array.
Replace:

"discountSelected": [
  "{{discounts}}"
]

by:

"discountSelected": [
]

Finally, set the resulting payload as a local variable.

Then, in the Body section , select raw, format JSON, and specify in it the local variable initialized in the Pre-request Script section.

Here is the Pre-request Script section:

function replaceIterationVar(variableName, listEmptyVars) {
  var variableValue = pm.iterationData.get(variableName);
  if (variableValue !== "") {
    return variableValue;
  }
  else {
    listEmptyVars.push(variableName);
    return `{{${variableName}}}`;
  }
}

function replaceIterationVars(payloadStr, callbackEmptyVarMap) {
  var listEmptyVars = [];
  payloadStr = payloadStr.replace(/{{(.+)}}/g, function (str, elm) {
    return replaceIterationVar(elm, listEmptyVars);
  });
  if (listEmptyVars.length > 0) {
    var payloadObj = JSON.parse(payloadStr);
    listEmptyVars.forEach(varName => {
      var f = callbackEmptyVarMap[varName];
      if (f) {  
        f(payloadObj);
      }
    });
    payloadStr = JSON.stringify(payloadObj);
  }
  return payloadStr;
}

var payloadStr = `{
  "productCode": [
    "safecoPet"
  ],
  "primaryResidence": {
    "state": "{{state}}",
    "postalCode": "{{postalCode}}"
  },
  "pets": [
    {
      "name": "Charlie",
      "species": "{{species}}",
      "breed": "{{breed}}",
      "age": {
        "unit": "{{unit}}",
        "value": {{value}}
      },
      "gender": "{{gender}}",
      "birthDate": "2018-09-03",
      "altered": true
    }
  ],
  "productOptions": {
    "reimbursement": {{reimbursement}},
    "deductible": {{deductible}},
    "annualLimit": {{annualLimit}},
    "coverageTypesSelected": [
      "Wellness"
    ],
    "discountSelected": [
      "{{discounts}}"
    ]
  }
}`;

var callbackEmptyVarMap = {
    "discounts": function (payloadObj) { payloadObj.productOptions.discountSelected = []; }
}

payloadStr = replaceIterationVars(payloadStr, callbackEmptyVarMap);
pm.variables.set("payload", payloadStr);

And here is the Body section, format raw, JSON:

{{payload}}
1 Like