# Body matches string validations for array of Objects

**URL:** <https://community.postman.com/t/body-matches-string-validations-for-array-of-objects/13759>\
**Category:** Help Hub\
**Created:** [June 30, 2020, 10:42am UTC](https://community.postman.com/t/body-matches-string-validations-for-array-of-objects/13759 "2020-06-30T10:42:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudarshan.ohal](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@sudarshan.ohal](https://community.postman.com/u/sudarshan.ohal)\
**Post date:** [June 30, 2020, 10:42am UTC](https://community.postman.com/t/body-matches-string-validations-for-array-of-objects/13759/1 "2020-06-30T10:42:52Z")

</div>

I have a response in below format.

```
[
    {
        "Name": "PQR",
        "Code": "000XXY",
        "Text": "ssdd1",
        "Charg1": {
            "step": 1,
            "Months": 3,
            "endDate": "2099-09-01T00:00:00",
            "percent": 10
        },
        "Fees1": false
    },
    {
        "Name": "ABC",
        "Code": "000XXY",
        "Text": "assd2 ",
        "Charg2": {
            "step": 2,
            "Months": 3,
            "endDate": "2099-09-01T00:00:00",
            "percent": 10
        },
        "Fees2": false
    },
]

```

I have tried below test scripts to validate the objects using below functions

```
var jsonData = JSON.parse(responseBody);
console.log(jsonData.length);

     pm.test("Body matches string from response body",function()
    {   
        for (i=0; i<jsonData.length;i++)
        {
            pm.expect(jsonData[i].text).to.include("Name");
        }

    });

```

Getting Below error message

Body matches string from response body | AssertionError: object tested must be an array, a map, an object, a set, a string, or a weakset, but undefined given

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [June 30, 2020, 11:01am UTC](https://community.postman.com/t/body-matches-string-validations-for-array-of-objects/13759/2 "2020-06-30T11:01:29Z")

</div>

Hey @sudarshan.ohal

Welcome to the Postman community! ⭐

I’m not 100% clear what you’re trying to check for but it looks like you’re trying to check that the `Name` property is part of each `object`.

You could do that like this:

```
pm.test("Body matches string from response body", function() {   
	for (i=0; i < jsonData.length; i++) {
		pm.expect(jsonData[i]).to.have.property("Name");
	}
});

```

The reference with had in the `pm.expect()` statement _jsonData[i].text_, doesn’t exist in the object so it would be `undefined`.

This assertion would be looking for the property in the object called `text` and checking that the value of that included the string `Name`.

```
pm.expect(jsonData[i].text).to.include("Name");
```

---

<div class="post-metadata">

**Author:** ![sudarshan.ohal](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@sudarshan.ohal](https://community.postman.com/u/sudarshan.ohal)\
**Post date:** [June 30, 2020, 11:17am UTC](https://community.postman.com/t/body-matches-string-validations-for-array-of-objects/13759/3 "2020-06-30T11:17:54Z")

</div>

@danny-dainton: Thanks for the quick help. This is working for me.
