Hello everyone
I have this response below from a GET call where I need to validate and iterate through the priceOptions object.
Since the response object can be quite long, I’m trying to iterate through each price object and double check the price label, price value type, string vs number etc…
There are few tests that I want to perform in each priceOption object, but I need the test to check the first object then move on to the next and so on.
Any assistance is much appreciated
Have anyone of you come across anything similar?
CURRENT TEST SCRIPT BUT NOT ITERATING/WORKING THROUGH
NOTE: THIS SCRIPT DOES NOT NECESSARILY NEED TO BE USED, FEEL FREE TO RECOMMEND A NEW SCRIPT
//This sets the key at 5
pm.environment.set(“key”, “5”);
//Getting the environment variable
var key = postman.getEnvironmentVariable(“key”);
//decreasing by one so we can go through the next key
key–;
//Settting the key for the next iteration
postman.setEnvironmentVariable(“key”, key);
//Function to get the list
function findObjectContaininsLists(sessions) {
// Iterate over the properties (keys) in the object
for (var key in sessions) {
// If the property is priceOptions, return the priceOptions object
if (sessions[key].hasOwnProperty('priceOptions')) {
return sessions[key].priceOptions[key];
}
}
}
//Running the test
pm.test(“Check status”, function () {
// Parse JSON body
var jsonData = pm.response.json();
// Retrieve the priceOptions object
var priceOptions = findObjectContaininsLists(jsonData.sessions);
//Verifying to see if it is a string
pm.expect(priceOptions.label).to.be.a(‘string’);
console.log(priceOptions.label)
});
RESPONSE
{
“sessions”: [
{
“startTimeLocal”: “2020-09-01 08:00:00”,
“endTimeLocal”: “2020-09-01 09:00:00”,
“seatsAvailable”: 999,
“priceOptions”: [
{
“label”: “Adult”,
“price”: 2.11,
“seatsUsed”: 1,
“minQuantity”: 1,
“maxQuantity”: 50
},
{
“label”: “Child”,
“price”: 1.27,
“seatsUsed”: 1,
“minQuantity”: 0,
“maxQuantity”: 50
}
]
},
{
“startTimeLocal”: “2020-09-01 08:30:00”,
“endTimeLocal”: “2020-09-01 09:30:00”,
“seatsAvailable”: 999,
“priceOptions”: [
{
“label”: “Adult”,
“price”: 2.11,
“seatsUsed”: 1,
“minQuantity”: 1,
“maxQuantity”: 50
},
{
“label”: “Child”,
“price”: 1.27,
“seatsUsed”: 1,
“minQuantity”: 0,
“maxQuantity”: 50
}
]
},
{
“startTimeLocal”: “2020-09-01 09:00:00”,
“endTimeLocal”: “2020-09-01 10:00:00”,
“seatsAvailable”: 999,
“priceOptions”: [
{
“label”: “Adult”,
“price”: 2.11,
“seatsUsed”: 1,
“minQuantity”: 1,
“maxQuantity”: 50
},
{
“label”: “Child”,
“price”: 1.27,
“seatsUsed”: 1,
“minQuantity”: 0,
“maxQuantity”: 50
}
]
},
{
“startTimeLocal”: “2020-09-02 21:00:00”,
“endTimeLocal”: “2020-09-02 22:00:00”,
“seatsAvailable”: 999,
“priceOptions”: [
{
“label”: “Adult”,
“price”: 2.11,
“seatsUsed”: 1,
“minQuantity”: 1,
“maxQuantity”: 50
},
{
“label”: “Child”,
“price”: 1.27,
“seatsUsed”: 1,
“minQuantity”: 0,
“maxQuantity”: 50
}
]
}
]
}