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

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!

You need to use an IF statement.

Similar to the following…

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.

1 Like

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.

I managed to do it by a slight change:

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

I works now. Thanks for the advice!