Iterate through json response and validate objects, keys and values asserting whether field type is string or number etc

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
}
]
}
]
}

If you just want to validate the structure of the response, you may want to look into schema validation as this will dramatically reduce your code complexity.

1 Like

Thanks for the feedback @vdespa I tried to use AJV but I was expecting the validation of the seatsAvailable to give me an error since the response is a number but on the validation below I was expecting a String…Not sure what is wrong here

var Ajv = require(‘ajv’),

ajv = new Ajv({logger: console}),

accessSchema = {

"type": "object",

"sessions": {

    "type": "array",

    "properties": {

        "seatsAvailable": {"type": "string"}

    }

}

}

pm.test(‘Schema is valid’, function() {

pm.expect(ajv.validate(accessSchema, pm.response.json())).to.be.true;

});

There is still something missing in your schema. Please watch these two tutorials to understand the approach to writing a schema that works.

2 Likes