# How to write to the console log in Postman when a choice is defined from an underlying table and when there isn't

**URL:** <https://community.postman.com/t/how-to-write-to-the-console-log-in-postman-when-a-choice-is-defined-from-an-underlying-table-and-when-there-isnt/48773>\
**Category:** Help Hub\
**Tags:** tests, console\
**Created:** [June 21, 2023, 11:46am UTC](https://community.postman.com/t/how-to-write-to-the-console-log-in-postman-when-a-choice-is-defined-from-an-underlying-table-and-when-there-isnt/48773 "2023-06-21T11:46:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robsegeren](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@robsegeren](https://community.postman.com/u/robsegeren)\
**Post date:** [June 21, 2023, 11:46am UTC](https://community.postman.com/t/how-to-write-to-the-console-log-in-postman-when-a-choice-is-defined-from-an-underlying-table-and-when-there-isnt/48773/1 "2023-06-21T11:46:54Z")

</div>

I wrote a test in Postman which works when a choice is made from the underlying table. The chosen data is then fetched and written to the console log for the next request.  
For example as shown in the console log: “value of standardFunction:Greece”

I also successfully wrote a test (almost the same one) for when for a certain field no choice was made from the underlying table. This will be null in the console log, which is fine.  
For example as shown in the console log: “value of standardFunction:null”

But I need to choose between those tests and upfront I don’t always know if there will be data chosen from the underlying table or not.

This works in case there is data to be fetched:  
bodydata = JSON.parse (responseBody) // to parse json response  
valuestandardFunction = bodydata.\_embedded[0].standardFunction.value  
console.log (“value of standardFunction:” + valuestandardFunction) pm.environment.set(“standaardfunctie”, valuestandardFunction);

This works when there is no data to be fetched:  
bodydata = JSON.parse (responseBody) // to parse json response  
valuestandardFunction = bodydata.\_embedded[0].standardFunction  
console.log (“value of standardFunction:” + valuestandardFunction) pm.environment.set(“standaardfunctie”, valuestandardFunction);

The only difference between the two is the .value in the second row.

My question is; what do I need to change to my test so that it sees the difference between there being data and null? So that I can use this test in both scenarios.

I really hope someone can help, thanks in advance!

---

<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:** [June 21, 2023, 4:47pm UTC](https://community.postman.com/t/how-to-write-to-the-console-log-in-postman-when-a-choice-is-defined-from-an-underlying-table-and-when-there-isnt/48773/2 "2023-06-21T16:47:00Z")

</div>

You need to use an IF statement.

Similar to the following…

```auto
const response = pm.response.json(); // parse response

let standardFunction = response._embedded[0].standardFunction // define this as a variable so the IF statement is easier to read

if ("value" in standardFunction) { // does the value 'key' exist?
    valuestandardFunction = standardFunction.value;
} else {
    valuestandardFunction = standardFunction;
}

console.log ("value of standardFunction:" + valuestandardFunction) 
pm.environment.set("standaardfunctie", valuestandardFunction);

```

On a side note, you don’t appear to have any actual tests\assertions in your current code. (pm.test and pm.expect blocks).

Now although that will probably work for you. My recommendation is that you don’t have IF statements like this.

You need to be in control of your test data and it should return the same response each and every time.

This should be two specific requests to the same API endpoint. With appropriate tests for existence of value and the second request checking that it doesn’t exist.

---

<div class="post-metadata">

**Author:** ![robsegeren](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@robsegeren](https://community.postman.com/u/robsegeren)\
**Post date:** [June 22, 2023, 6:36am UTC](https://community.postman.com/t/how-to-write-to-the-console-log-in-postman-when-a-choice-is-defined-from-an-underlying-table-and-when-there-isnt/48773/3 "2023-06-22T06:36:47Z")

</div>

This does work in case there is a value. But if there isn’t then the following error appears:

TypeError: Cannot use ‘in’ operator to search for ‘value’ in null

Its like it is expecting a value per se but it should check if there is and if not, then it should write null to the console log.

---

<div class="post-metadata">

**Author:** ![robsegeren](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@robsegeren](https://community.postman.com/u/robsegeren)\
**Post date:** [June 22, 2023, 9:11am UTC](https://community.postman.com/t/how-to-write-to-the-console-log-in-postman-when-a-choice-is-defined-from-an-underlying-table-and-when-there-isnt/48773/4 "2023-06-22T09:11:04Z")

</div>

I managed to do it by a slight change:

if (standardFunction != null) { // does the value ‘key’ exist?

I works now. Thanks for the advice!
