# How to validate attributes in response

**URL:** <https://community.postman.com/t/how-to-validate-attributes-in-response/44421>\
**Category:** Help Hub\
**Tags:** scripts, response, schemavalidation\
**Created:** [February 24, 2023, 8:15am UTC](https://community.postman.com/t/how-to-validate-attributes-in-response/44421 "2023-02-24T08:15:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vbadgujarcsod](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@vbadgujarcsod](https://community.postman.com/u/vbadgujarcsod)\
**Post date:** [February 24, 2023, 8:15am UTC](https://community.postman.com/t/how-to-validate-attributes-in-response/44421/1 "2023-02-24T08:15:06Z")

</div>

How to validate attributes in response. Refer Screenshot

 ![Screenshot 2023-02-24 at 1.40.18 PM](https://us1.discourse-cdn.com/getpostman/original/3X/4/9/49854b5505992ab7d531df7c798f45954508e358.png)

I would like to validate all attributes/keys present in training\_details array like start date, end date etc [Note: I dont want to validate value of attributes/keys]

---

<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:** [February 24, 2023, 11:22am UTC](https://community.postman.com/t/how-to-validate-attributes-in-response/44421/2 "2023-02-24T11:22:21Z")

</div>

Subtle difference but the training\_details in your screenshot is a object, not an array.

Curly brackets are objects, square brackets are arrays.

It’s important to know the difference as certain JavaScript function are only available on arrays.

Moving on from that, I present you with three options for you to try.

```auto
let response = pm.response.json() // parse to just under the training_details object.

// method 1 - to.have.property

let keysToCheck = ["id", "start_date", "end_date", "invalid_key"];

keysToCheck.forEach(key => {
    pm.test(`${key} exists in training_details V2`, () => {
        pm.expect(response.training_details).to.have.property(key);
    });
});

// method 2 - object keys & to.include

let keys = Object.keys(response.training_details);

keysToCheck.forEach(keyToCheck => {
    pm.test(`${keyToCheck} exists in training_details V1`, () => {
        pm.expect(keys).to.include(keyToCheck);
    });
});

// method 3 - JSON Schema.

const schema = {
    "type": "object",
    "properties": {
        "training_details": {"type": "object",
            "properties": {
                "id": { "type": "string"},
                "start_date": { "type": "string"},
                "end_date": { "type": "string"},
            },
        "required": ["id", "start_date","end_date"]
        }
    },
};

pm.test('API response schema is valid', () => {
    pm.expect(response).to.have.jsonSchema(schema);
});

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/4/74c9b2966ec68289c11259917397fcfdf7817cef.png)

---

<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:** [November 26, 2023, 7:04pm UTC](https://community.postman.com/t/how-to-validate-attributes-in-response/44421/3 "2023-11-26T19:04:15Z")

</div>

Closed on November 26, 2023, 7:04pm UTC

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