# Checking fields in an array using a for loop

**URL:** <https://community.postman.com/t/checking-fields-in-an-array-using-a-for-loop/52847>\
**Category:** Help Hub\
**Tags:** variables, json, array\
**Created:** [October 5, 2023, 8:36am UTC](https://community.postman.com/t/checking-fields-in-an-array-using-a-for-loop/52847 "2023-10-05T08:36:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![satellite-geologis10](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@satellite-geologis10](https://community.postman.com/u/satellite-geologis10)\
**Post date:** [October 5, 2023, 8:36am UTC](https://community.postman.com/t/checking-fields-in-an-array-using-a-for-loop/52847/1 "2023-10-05T08:36:52Z")

</div>

I want to check if certain fields are in the response body. I tried to use a for loop and an array with the names of these fields.

This is my test code:

```auto
const jsonData = pm.response.json();
const responce_required = [
    "guest_email",
    "status"
];
const x = responce_required.length;
// Check required fields in responce body
pm.test("Verify that all required fields in the response body are exist", () => {
    for (let i = 0; i < x; i++) {
        pm.expect(jsonData.responce_required[i]).to.exist; 
    }

```

Then I get the following error:  
`TypeError: jsonData.responce_required is undefined`

What could be wrong?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [October 5, 2023, 9:58am UTC](https://community.postman.com/t/checking-fields-in-an-array-using-a-for-loop/52847/2 "2023-10-05T09:58:34Z")

</div>

This should be done using the jsonSchema assertion.

```auto
pm.test('validate JSON Schema', function() {
    pm.expect(response).to.have.jsonSchema(schema);
});

```

First of all, you have to define the schema as a variable. (Please use the preformatted text option in the editor so it doesn’t lose its formatting and align everything to the left).

If you need help with this, please post an example response.

---

<div class="post-metadata">

**Author:** ![satellite-geologis10](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@satellite-geologis10](https://community.postman.com/u/satellite-geologis10)\
**Post date:** [October 6, 2023, 6:53am UTC](https://community.postman.com/t/checking-fields-in-an-array-using-a-for-loop/52847/3 "2023-10-06T06:53:23Z")

</div>

I was able to compare the entire created scheme with the response, as you wrote ( pm.expect(response).to.have.jsonSchema(schema);). But how can I compare individual fields? To compare a response field with a specific field with the value (string - name of the required field) from my array? Like

```auto
pm.expect(response).to.have.jsonSchema(schema);

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [October 6, 2023, 10:03am UTC](https://community.postman.com/t/checking-fields-in-an-array-using-a-for-loop/52847/5 "2023-10-06T10:03:40Z")

</div>

JSON is based on key\value pairs. (Field = key).

If you compare the JSON Schema, it will check every key at once.

If any fields are missing, then it will fail the test.

You also have options in the schema for checking for additional fields and failing in that scenario as well.

It won’t check the values of the fields, but can check the format. Boolean, string, integer, etc.

You still haven’t included a sample response. As sometimes JSON can be complex with objects in objects, within arrays. Which can make the JSONSchema complex to get right.

---

<div class="post-metadata">

**Author:** ![satellite-geologis10](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@satellite-geologis10](https://community.postman.com/u/satellite-geologis10)\
**Post date:** [October 7, 2023, 6:19pm UTC](https://community.postman.com/t/checking-fields-in-an-array-using-a-for-loop/52847/6 "2023-10-07T18:19:09Z")

</div>

Thank you! As I understand it, this is the best way.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [October 10, 2023, 6:19pm UTC](https://community.postman.com/t/checking-fields-in-an-array-using-a-for-loop/52847/7 "2023-10-10T18:19:16Z")

</div>

Closed on October 10, 2023, 6:19pm UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
