# Console Log Issue with expected Fields not in the response

**URL:** <https://community.postman.com/t/console-log-issue-with-expected-fields-not-in-the-response/42594>\
**Category:** Help Hub\
**Tags:** console\
**Created:** [January 4, 2023, 8:31pm UTC](https://community.postman.com/t/console-log-issue-with-expected-fields-not-in-the-response/42594 "2023-01-04T20:31:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![perezb2022](https://avatars.discourse-cdn.com/v4/letter/p/e495f1/32.png) [@perezb2022](https://community.postman.com/u/perezb2022)\
**Post date:** [January 4, 2023, 8:31pm UTC](https://community.postman.com/t/console-log-issue-with-expected-fields-not-in-the-response/42594/1 "2023-01-04T20:31:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 5, 2023, 1:08pm UTC](https://community.postman.com/t/console-log-issue-with-expected-fields-not-in-the-response/42594/2 "2023-01-05T13:08:42Z")

</div>

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.

```auto
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;
});

```

---

<div class="post-metadata">

**Author:** ![perezben](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@perezben](https://community.postman.com/u/perezben)\
**Post date:** [January 5, 2023, 3:29pm UTC](https://community.postman.com/t/console-log-issue-with-expected-fields-not-in-the-response/42594/3 "2023-01-05T15:29:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 5, 2023, 3:55pm UTC](https://community.postman.com/t/console-log-issue-with-expected-fields-not-in-the-response/42594/4 "2023-01-05T15:55:41Z")

</div>

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.

```auto
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    
)

```

---

<div class="post-metadata">

**Author:** ![perezben](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@perezben](https://community.postman.com/u/perezben)\
**Post date:** [January 5, 2023, 4:14pm UTC](https://community.postman.com/t/console-log-issue-with-expected-fields-not-in-the-response/42594/5 "2023-01-05T16:14:40Z")

</div>

Thank you , I will look into it  
regards

---

<div class="post-metadata">

**Author:** ![perezben](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@perezben](https://community.postman.com/u/perezben)\
**Post date:** [January 6, 2023, 8:57pm UTC](https://community.postman.com/t/console-log-issue-with-expected-fields-not-in-the-response/42594/6 "2023-01-06T20:57:21Z")

</div>

> [@michaelderekjones](#):
>
> ```auto
> else {
> response.taxAttributes = [{"attributeType": "null","attributeValue": "null"}]
> }
> 
> ```

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”: “nul`Preformatted text`l”}]  
}

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 8, 2023, 4:52pm UTC](https://community.postman.com/t/console-log-issue-with-expected-fields-not-in-the-response/42594/7 "2023-01-08T16:52:47Z")

</div>

> [@perezben](#):
>
> 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

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

> [@How to add nested object to existing json](https://community.postman.com/t/how-to-add-nested-object-to-existing-json/42629/2):
>
> You have to stringify the responseData before saving it to the “body” environment variable. (Like you do for the updatedBody). Then parse it when you retrieve it. You can parse this in one line. let variableName = JSON.parse(pm.environment.get("body")); If you use let or var instead of const, then you can update the elements directly. You don’t really need to use push. PS: This is a JavaScript object at this point. In your example, the product.push is incorrect, as surely it should be j…
