How do I choose a date that has the value of the parameter "is_available" = true?

My question: I need the postman to view the response and add to the value of the variable date_visit the date that has the value of the parameter is_available == true. Please help me

Details (like screenshots):
Response body looks like:
[
{
“date”: “2023-05-08”,
“is_available”: false
},
{
“date”: “2023-05-09”,
“is_available”: true
},
{
“date”: “2023-05-10”,
“is_available”: true
},
{
“date”: “2023-05-11”,
“is_available”: true
},
{
“date”: “2023-05-12”,
“is_available”: true
},
{
“date”: “2023-05-13”,
“is_available”: true
},
{
“date”: “2023-05-14”,
“is_available”: true
},
{
“date”: “2023-05-15”,
“is_available”: true
},
{
“date”: “2023-05-16”,
“is_available”: true
},
{
“date”: “2023-05-17”,
“is_available”: true
},
{
“date”: “2023-05-18”,
“is_available”: true
},
{
“date”: “2023-05-19”,
“is_available”: true
},
{
“date”: “2023-05-20”,
“is_available”: true
},
{
“date”: “2023-05-21”,
“is_available”: true
},
{
“date”: “2023-05-22”,
“is_available”: true
},
{
“date”: “2023-05-23”,
“is_available”: true
},
{
“date”: “2023-05-24”,
“is_available”: true
},
{
“date”: “2023-05-25”,
“is_available”: true
},
{
“date”: “2023-05-26”,
“is_available”: true
},
{
“date”: “2023-05-27”,
“is_available”: true
},
{
“date”: “2023-05-28”,
“is_available”: true
},
{
“date”: “2023-05-29”,
“is_available”: false
},
{
“date”: “2023-05-30”,
“is_available”: false
},
{
“date”: “2023-05-31”,
“is_available”: false
},
{
“date”: “2023-06-01”,
“is_available”: false
},
{
“date”: “2023-06-02”,
“is_available”: false
},
{
“date”: “2023-06-03”,
“is_available”: false
},
{
“date”: “2023-06-04”,
“is_available”: false
},
{
“date”: “2023-06-05”,
“is_available”: false
},
{
“date”: “2023-06-06”,
“is_available”: false
},
{
“date”: “2023-06-07”,
“is_available”: false
}
]

I’ve already tried:
var responseObject = JSON.parse(responseBody);
if(responseObject[0].is_available ** true)
{
pm.environment.set(“date_visit”, responseObject[0].date);
}
but it doesnt work

You have an array of objects in your response.

The first element in that array has “is_available” set to false, hence your IF statement won’t be triggered.

However, there is more than one object that has “is_available” set to true

Which one needs to be added to the “date_visit” environment variable?

The first one? The last one? All of them?

In the response, each time different values will come for the “is_available” element, I need the system to select the first “date_visit” element, which “is_available” will be true.
For example:
[
{
“date”: “2023-05-08”,
“is_available”: false
},
{
“date”: “2023-05-09”,
“is_available”: true
},
{
“date”: “2023-05-10”,
“is_available”: true
}
The system should take the date “2023-05-09” and make it a variable.
And if the first date has “is_available”: true, then it will take it as a variable. If the first three are false, it will take the fourth one if it has “is_available”: true.
Any first element “Date” that has “is available” equal to “true”

This can be achieved using the JavaScript find function.

This will return the first element that meets the criteria.

JavaScript Array find() Method (w3schools.com)

If you wanted all of the is_available results, it would be the JavaScript filter function instead.

const response = pm.response.json();

let search = (response.find(obj => {return obj.is_available === true})).date; // "2023-05-09"
console.log(search);

pm.test("Date is correct", () => {
    pm.expect(search).to.be.a("string");
    pm.expect(search).to.match(/^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$/);
    pm.environment.set("date_visit", search);
})

The regex checking the date comes from the following.

java - Regex date validation for yyyy-mm-dd - Stack Overflow

Put the setting of the environment variable after the assertions. If the assertions fail, then it won’t try to set the variable.

Thank you very much! It helped a lot :mending_heart: