# Assertion for checking key name (array) in request body?

**URL:** <https://community.postman.com/t/assertion-for-checking-key-name-array-in-request-body/28046>\
**Category:** Help Hub\
**Tags:** newman-project, tests, json, responseassertion\
**Created:** [September 22, 2021, 10:07am UTC](https://community.postman.com/t/assertion-for-checking-key-name-array-in-request-body/28046 "2021-09-22T10:07:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![trangntt19hcm](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@trangntt19hcm](https://community.postman.com/u/trangntt19hcm)\
**Post date:** [September 22, 2021, 10:07am UTC](https://community.postman.com/t/assertion-for-checking-key-name-array-in-request-body/28046/1 "2021-09-22T10:07:30Z")

</div>

I have **Request Body** as below (Reality, I will request more than 100 key names ) and I want to check all key names requested are present or not? But I got a problem with handling array “errors” =\> Have any advice for me? Please help me. Thank you!

```auto
{
"user_id": 123,
"status": "completed",
"errors":[
        {
            "error_code": "E01",
            "error_message": "Wrong img"
        }
    ]
}

```

My assertion as below but it cannot check these key name: error\_code, error\_message are present and correct or not.  
let requestBody = JSON.parse(pm.request.body);  
pm.test(“Check keys of request body”, function () {  
pm.expect(requestBody).to.have.keys([‘user\_id’,‘status’,‘errors’]);  
});

---

<div class="post-metadata">

**Author:** ![neilstudd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/neilstudd/32/8213_2.png) [@neilstudd](https://community.postman.com/u/neilstudd)\
**Post date:** [September 22, 2021, 8:40pm UTC](https://community.postman.com/t/assertion-for-checking-key-name-array-in-request-body/28046/2 "2021-09-22T20:40:34Z")

</div>

Hi @trangntt19hcm,

If you include this code within your test, it will loop through all of the items in the “errors” list, and check that these keys are present for every error. (If there are no errors present, the loop will be skipped.)

```auto
for (var i=0; i < requestBody.errors.length; i++) {
    pm.expect(requestBody.errors[i]).to.have.keys(['error_code','error_message']);    
}

```

---

<div class="post-metadata">

**Author:** ![trangntt19hcm](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@trangntt19hcm](https://community.postman.com/u/trangntt19hcm)\
**Post date:** [September 23, 2021, 2:57am UTC](https://community.postman.com/t/assertion-for-checking-key-name-array-in-request-body/28046/3 "2021-09-23T02:57:43Z")

</div>

Thanks for your help,  
May I ask you one more case?  
My **Request Body** as below =\> I want to check whether all keys in request body (id, status, account/acc\_code, account/acc\_cate, account/acc\_name, account/payment/pay\_code, account/payment/pay\_name, unit) are present or not? How do I should? Have any Arrsertion which support check all keys name presented at a time?

```auto
{
"id": 123,
"status": "completed",
"account":
        {
            "acc_code": "AC01",
            "acc_cate": 1,
			"acc_name":"ABC",
			"payment":
					{
						"pay_code":"PM01",
						"pay_name":"XYZ",
					}
		    .
			.
			.
			.
        },
"unit":
		{
			

		}
}

Currently, I have a solution as below => Is it correct? whether have an Arrsetion check all keys in a time? Have any advice for me?
let jsonReq = JSON.parse(pm.request.body);
pm.test("Test array L1", () => {
	pm.expect(jsonReq).to.have.keys(['id','account','unit']); 
}); 

pm.test("Test array L2", () => {
   for (var i=0; i < jsonReq.account.length; i++) {
    pm.expect(jsonReq.account[i]).to.have.keys(['acc_code','acc_cate','acc_name','payment']); 
}
}); 

pm.test("Test array L3", () => {
   for (var i=0; i < jsonReq.account.payment.length; i++) {
    pm.expect(jsonReq.account.payment[i]).to.have.keys(['pay_code','pay_name']); 
}
}); 

```
