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

**URL:** <https://community.postman.com/t/iterate-through-json-response-and-validate-objects-keys-and-values-asserting-whether-field-type-is-string-or-number-etc/15261>\
**Category:** Help Hub\
**Tags:** documentation, tests, variables, collections\
**Created:** [August 24, 2020, 10:57am UTC](https://community.postman.com/t/iterate-through-json-response-and-validate-objects-keys-and-values-asserting-whether-field-type-is-string-or-number-etc/15261 "2020-08-24T10:57:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rdgtxr](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rdgtxr/32/5356_2.png) [@rdgtxr](https://community.postman.com/u/rdgtxr)\
**Post date:** [August 24, 2020, 10:57am UTC](https://community.postman.com/t/iterate-through-json-response-and-validate-objects-keys-and-values-asserting-whether-field-type-is-string-or-number-etc/15261/1 "2020-08-24T10:57:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [August 27, 2020, 1:25pm UTC](https://community.postman.com/t/iterate-through-json-response-and-validate-objects-keys-and-values-asserting-whether-field-type-is-string-or-number-etc/15261/2 "2020-08-27T13:25:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rdgtxr](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rdgtxr/32/5356_2.png) [@rdgtxr](https://community.postman.com/u/rdgtxr)\
**Post date:** [August 27, 2020, 10:35pm UTC](https://community.postman.com/t/iterate-through-json-response-and-validate-objects-keys-and-values-asserting-whether-field-type-is-string-or-number-etc/15261/3 "2020-08-27T22:35:02Z")

</div>

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;

```

});

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [August 28, 2020, 5:30am UTC](https://community.postman.com/t/iterate-through-json-response-and-validate-objects-keys-and-values-asserting-whether-field-type-is-string-or-number-etc/15261/4 "2020-08-28T05:30:09Z")

</div>

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

[![](https://img.youtube.com/vi/haDQBmQii2g/maxresdefault.jpg "JSON Schema validation in Postman") ](https://www.youtube.com/watch?v=haDQBmQii2g)

[![](https://img.youtube.com/vi/P_So0vpNJCQ/maxresdefault.jpg "JSON Schema validation (nested objects, arrays) in Postman - Part II") ](https://www.youtube.com/watch?v=P_So0vpNJCQ)
