# Need help looping through an object and checking the keys

**URL:** <https://community.postman.com/t/need-help-looping-through-an-object-and-checking-the-keys/27494>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [September 4, 2021, 10:53pm UTC](https://community.postman.com/t/need-help-looping-through-an-object-and-checking-the-keys/27494 "2021-09-04T22:53:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cryosat-operator-620](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@cryosat-operator-620](https://community.postman.com/u/cryosat-operator-620)\
**Post date:** [September 4, 2021, 10:53pm UTC](https://community.postman.com/t/need-help-looping-through-an-object-and-checking-the-keys/27494/1 "2021-09-04T22:53:10Z")

</div>

_My question_: Hey I have a single json object that is returned and I want to be able to test that only the allowed keys are returned in that object, since the data will be different each time I only want to check the keys

Here is the object structure

[  
{  
“Id”: “75725702-50d8-41a8-963d-4aaaaaaaaaaa”,  
“Number”: “500200”,  
“Key”: 565465454,  
“CategoryId”: 5,  
“Option”: “5”,  
“CreatedByUserId”: “4127”,  
“CreatedDate”: “2020-08-04T00:38:44.988+12:00”,  
“LastModifiedByUserId”: “4127”,  
“LastModifiedDate”: “2020-08-04T00:38:44.988+12:00”  
}  
]

Here is the test progress I have so far - not much

pm.test(“Object only contains allowed keys”, () =\> {

var notesJsonStructure = [‘Id’, ‘Number’, ‘Key’, ‘CategoryId’, ‘Option’, ‘CreatedByUserId’, ‘CreatedDate’, ‘LastModifiedByUserId’, ‘LastModifiedDate’];

var responseObj = pm.response.json();

console.log(responseObj);

```
_.each(responseObj.response, (item) => {

    console.log("Test" + item);
    pm.expect(item).to.be.oneOf(notesJsonStructure);       
});

```

});

The line of console.log(“Test” + item); never outputs, so makes me think I am not even hitting that far

Thanks

---

<div class="post-metadata">

**Author:** ![cryosat-operator-620](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@cryosat-operator-620](https://community.postman.com/u/cryosat-operator-620)\
**Post date:** [September 4, 2021, 11:19pm UTC](https://community.postman.com/t/need-help-looping-through-an-object-and-checking-the-keys/27494/2 "2021-09-04T23:19:21Z")

</div>

Hmm seems like I could use schema validation but this fails as well

pm.test(“Object only contains allowed keys”, () =\> {

var schema = {  
“Properties”:  
{  
“Id”: {“type”: “string”},  
‘Number’, {“type”: “string”},  
“Key”: {“type”: int  
“CategoryId”: {“type”: “int”},  
“Note”: {“type”: “string”},  
“CreatedByUserId”: {“type”: “string”},  
“CreatedDate”: {“type”: “string”},  
“LastModifiedByUserId”: {“type”: “string”},  
“LastModifiedDate”: {“type”: “string”}  
}  
}  
var responseObj = pm.response.json();  
console.log(responseObj);  
console.log(schema);

pm.expect(responseObj,schema).to.be.true;  
});

---

<div class="post-metadata">

**Author:** ![cryosat-operator-620](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@cryosat-operator-620](https://community.postman.com/u/cryosat-operator-620)\
**Post date:** [September 4, 2021, 11:35pm UTC](https://community.postman.com/t/need-help-looping-through-an-object-and-checking-the-keys/27494/3 "2021-09-04T23:35:28Z")

</div>

I got it working with the below, will this cover the case were the sending API inserts new keys that I don;t have on my list? I just checked and it does.

pm.test(“Response body must have valid keys”, function () {

var data = pm.response.json();

```
var notesJsonStructure = ['Id', 'Number', 'Key', 'CategoryId', 'Option', 'CreatedByUserId', 'CreatedDate', 'LastModifiedByUserId', 'LastModifiedDate'];

```

data.forEach(function(item) {

```
  pm.expect(item).to.have.all.keys(notesJsonStructure);     

```

});

});

While this works I would prefer schema validation most probably

---

<div class="post-metadata">

**Author:** ![murdoc.trammell](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@murdoc.trammell](https://community.postman.com/u/murdoc.trammell)\
**Post date:** [September 9, 2021, 3:49pm UTC](https://community.postman.com/t/need-help-looping-through-an-object-and-checking-the-keys/27494/4 "2021-09-09T15:49:51Z")

</div>

Your instinct that this should be done by schema validation is spot on. The solution you posted is only happy path as far as I can tell - it won’t tell you about unexpected keys if all the expected ones are present.

---

<div class="post-metadata">

**Author:** ![cryosat-operator-620](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@cryosat-operator-620](https://community.postman.com/u/cryosat-operator-620)\
**Post date:** [September 17, 2021, 6:34am UTC](https://community.postman.com/t/need-help-looping-through-an-object-and-checking-the-keys/27494/5 "2021-09-17T06:34:52Z")

</div>

Kk so made progress, biggest issue was that I didn’t understand what I was working with, once I started checking what was been returned by doing this I was able to make real progress

pm.expect(responseObject.AvailableEids[0]).to.be.an(“object”);
