# Check each nested object contains a value in one of two objects

**URL:** <https://community.postman.com/t/check-each-nested-object-contains-a-value-in-one-of-two-objects/67755>\
**Category:** Help Hub\
**Tags:** tests, scripts, responseassertion\
**Created:** [August 31, 2024, 4:05am UTC](https://community.postman.com/t/check-each-nested-object-contains-a-value-in-one-of-two-objects/67755 "2024-08-31T04:05:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bethhalstead-meijer](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bethhalstead-meijer](https://community.postman.com/u/bethhalstead-meijer)\
**Post date:** [August 31, 2024, 4:05am UTC](https://community.postman.com/t/check-each-nested-object-contains-a-value-in-one-of-two-objects/67755/1 "2024-08-31T04:05:10Z")

</div>

Test that each nested and unnamed object has prior=248 or new=248

I am trying to write a test that checks each of the objects in the JSON response body for a specific string (in this case ‘248’) in either the ‘changes.prior’ or ‘changes.new’ field.

Here is an example JSON response body:

```auto
[
    {
        "eventType": "account-updated",
        "changes": [
            {
                "property": "preferredStoreId",
                "prior": "248",
                "new": "20"
            }
        ]
    },
    {
        "eventType": "account-updated",
        "changes": [
            {
                "property": "preferredStoreId",
                "prior": "265",
                "new": "248"
            }
        ]
    }
]

```

I reviewed [this post](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017) and attempted these test scripts, neither of which works:

```auto
pm.test("Each event contains search param", () => {
    require('lodash').each(jsonData, (item) => {
        pm.expect(item.changes.prior || item.changes.new).to.contain('248')
    })
})

pm.test('Each event contains search param', () => {
    require('lodash').each(jsonData, (item) => {
        pm.expect(item.changes.prior.contain("248") || item.changes.new.contain("248")).to.be.true
    })
})

```

I believe the outer [brackets] are confusing me. Can anyone help me with a test like this?

---

<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:** [August 31, 2024, 11:09am UTC](https://community.postman.com/t/check-each-nested-object-contains-a-value-in-one-of-two-objects/67755/2 "2024-08-31T11:09:31Z")

</div>

Changes is an array, so you need to target it with its array index.

Indexes start at zero, so it would be changes[0].

Postman uses [ChaiJS](https://www.chaijs.com/api/bdd/) for its assertion library, so its always useful to look at the examples.

The following test should meet your criteria.

```auto
const response = pm.response.json();

response.forEach((obj, i) => {
    let cNew = obj.changes[0].new; // new is a reserved word
    let prior = obj.changes[0].prior;
    pm.test(`Object ${i} prior[${prior}] or new[${cNew}] = 248 `, () => {
        pm.expect(new Set([cNew, prior])).to.include("248");
    })
});

```

I added a third object that didn’t meet the criteria so you could see the test failing.

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

I’m using the array index to customize the test case name, so you know which object has failed.

---

<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:** [September 3, 2024, 12:53pm UTC](https://community.postman.com/t/check-each-nested-object-contains-a-value-in-one-of-two-objects/67755/3 "2024-09-03T12:53:52Z")

</div>

Closed on September 3, 2024, 12:53pm UTC

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