Console Log Issue with expected Fields not in the response

Good day Gurus in Postman

I search for this all over the web and POSTMAN Forums but maybe did not enter the right Question.

I do have an issue with the Console Log .

For instance

I’m expecting in the response this segment “TaxAttributes” but in some cases this segment is not part of the response and comes like this “taxAttributes”:

“taxAttributes”: [
{
“attributeType”: “CST”,
“attributeValue”: “70”
}
which is ok but I have in my console log the field to be display every time and because the field or segments are not in some scenarios then console log does not display anything at all.

this is what I have in the console log to display the field value and works if the field comes in the response if not “SAD FACE”
data.taxLines[“0”].taxValues[“0”].taxAttributes[“1”].attributeType,
data.taxLines[“0”].taxValues[“0”].taxAttributes[“1”].attributeValue,

so the question is , Is there a way to tell console log that
IF EXIST
data.taxLines[“0”].taxValues[“0”].taxAttributes[“1”].attributeType then display
ELSE “field is not part of response”
" " "Display a tab or empy value "
ENDIF

console.info(“Counter:” + pm.iterationData.get(“COUNTER”),
data.taxLines[“0”].id,
** data.taxLines[“0”].totalTax,
data.taxLines[“0”].taxcode,
data.taxLines[“0”].taxValues[“0”].taxTypeCode,
)
** = something like this if totalTax is not in the response then display a Tab or " " in this way I will always have that column reserve for Totaltax

Command IF ELSE would be good but inside the Console Log

Thanks

The console log only gets written to if you code it to.

Therefore put your console.log within the IF statement.

Postman uses JavaScript, so you need to Google on “JavaScript IF, not equal”.

You can also test for empty arrays.

let response = pm.response.json().data;

if (response.taxAttributes[0]!= undefined){
    console.log(response.taxAttributes[0]);
}

else {
    console.log("taxAttributes is empty");
}

pm.test("Array check", () => {
    pm.expect(response.taxAttributes).to.be.an("array").that.is.not.empty;
});

Thank you @michaelderekjones for your response .
I do not want to split the console log because is a big response and I need it in 1 line
for instance .

I need to check on each field , if they are defined or not , that way I will always have the same column headers but values may be blank or something .

this final output is a csv file I did have everything working except now that I’m encontered this little “Big Issue” because Console is not displaying anything if 1 field is undefined .

Example

data.taxLines[“0”].taxValues[“0”].value,
data.taxLines[“0”].taxValues[“0”].otherBaseAmount,
data.taxLines[“0”].taxValues[“0”].taxAttributes[“0”].attributeType,
data.taxLines[“0”].taxValues[“0”].taxAttributes[“0”].attributeValue,

If (taxAttributes[“1”] = undefined) {
" " or TAB

else { data.taxLines[“0”].taxValues[“0”].taxAttributes[“1”].attributeType,
data.taxLines[“0”].taxValues[“0”].taxAttributes[“1”].attributeValue,
}
If (taxAttributes[“2”] = undefined) {
" " or TAB
else {
data.taxLines[“0”].taxValues[“0”].taxAttributes[“2”].attributeType,
data.taxLines[“0”].taxValues[“0”].taxAttributes[“2”].attributeValue,
});

Thanks for your time

I’m not really sure what you are doing.

If you parse the response using var or let instead of const, then you can change any element in the response before you do all of the console log activity.

You can do a quick IF statement on the element, and if its undefined, update it to include a space or whatever you want to include for undefined.

As taxAttributes is an array, you may need to include both the attributeType and attributeValue.

Please use the performatted text option in the editor when including code. Which stops everything getting aligned to the left.

The following example sets both elements to null.

let response = pm.response.json().data;

if (response.taxAttributes[0]!= undefined){
    console.log(response.taxAttributes[0]); // don't do anything here
}

else {
    response.taxAttributes = [{"attributeType": "null","attributeValue": "null"}]
}

console.info(
    response.taxAttributes[0].attributeType,
    response.taxAttributes[0].attributeValue    
)

Thank you , I will look into it
regards

Hello mdjones this can be a dummy question but

how about if I need to insert that array , not modify because it does not exist

else {
response.taxAttributes = [{“attributeType”: “null”,“attributeValue”: “nulPreformatted textl”}]
}

Have a look at the following topic which is doing just that.