# How can I test nested properties using JSON test data?

**URL:** <https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774>\
**Category:** Help Hub\
**Tags:** tests, collections, json, javascript\
**Created:** [June 3, 2024, 3:19pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774 "2024-06-03T15:19:02Z")\
**Posts on this page:** 9\
**Page:** 2

<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:** [June 5, 2024, 10:33am UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/21 "2024-06-05T10:33:06Z")

</div>

If you have 100 titles, then the best way is the forEach loop to test each title individually.

There are potential ways to test all titles in a single test, but when it fails it will be nearly impossible to tell which title failed (specially if more than one title is missing).

---

<div class="post-metadata">

**Author:** ![wilq8484](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@wilq8484](https://community.postman.com/u/wilq8484)\
**Post date:** [June 5, 2024, 2:44pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/22 "2024-06-05T14:44:31Z")

</div>

Can you let me know what is wrong here? I have `ReferenceError: title is not defined`.

Do I need to specify that ‘title’ as var not const, right after test data const?

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

let testData = pm.iterationData.get("testData")

pm.test(`${title} exists`, () => {
    let search = response.items.find(obj => obj.testData === title);
    pm.expect(search).to.not.be.undefined
})```
````

---

<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:** [June 5, 2024, 3:41pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/23 "2024-06-05T15:41:38Z")

</div>

If you get used to console logging your variables, hopefully this should shed light on your current problem, but yes you need to define a variable for title.

testData will be an array, so you need that forEach loop.

It’s within that loop you need to define the title variable.

---

<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:** [June 5, 2024, 4:03pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/24 "2024-06-05T16:03:34Z")

</div>

Based on the following response.

```auto
{
    "items": [
        {
            "title": "Title1"
        },
        {
            "title": "Title2"
        },
        {
            "title": "Title3"
        },
        {
            "title": "Title4"
        },
        {
            "title": "Title5"
        },
        {
            "title": "Title6"
        },
        {
            "title": "Title7"
        },
        {
            "title": "Title8"
        }
    ]
}

```

This should loop though your test data array, and then confirm that the title exists in the response.

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

let testData = pm.iterationData.get("testData");

testData.forEach(obj => {
    let title = obj.title
    pm.test(`${title} exists`, () => {
        let search = response.items.find(obj => obj.title === title);
        pm.expect(search, 'title search failed').to.not.be.undefined
        pm.expect("title" in search).to.be.true
        pm.expect(search.title).to.not.be.undefined
        pm.expect(search).to.have.property('title', title);
    })
});

```

I’ve included a few assertions for consideration. Checking that the search is not undefined first with a custom error message appears to be an appropriate method. I changed one of the titles in the test data to something that isn’t returned in response, so you can see how it looks.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/1/6/1643ee917acec2c090390a691c8a76b1e1379398.png)

---

<div class="post-metadata">

**Author:** ![wilq8484](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@wilq8484](https://community.postman.com/u/wilq8484)\
**Post date:** [June 5, 2024, 4:09pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/25 "2024-06-05T16:09:23Z")

</div>

Thanks again, will check it once will be back

---

<div class="post-metadata">

**Author:** ![wilq8484](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@wilq8484](https://community.postman.com/u/wilq8484)\
**Post date:** [June 5, 2024, 6:43pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/26 "2024-06-05T18:43:18Z")

</div>

fantastic, works excellent. However it must match exactly same title, is there a way to use ‘contains’ instead of equal?

as in my response I have e.g. Title1 2023, and I just need to make sure that ‘Title1’ value exists - if there is something extra like year, it can be ignored.

ah I noticed also that in my JSON test data titles, when I was converting CSV to JSON contained comma are ignored, I have to find a way to escape them 😉

---

<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:** [June 6, 2024, 12:53pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/27 "2024-06-06T12:53:42Z")

</div>

Taken from here…

[Can I use wildcards when searching an array of strings in Javascript? - Stack Overflow](https://stackoverflow.com/questions/12695594/can-i-use-wildcards-when-searching-an-array-of-strings-in-javascript)

```auto
testData.forEach(obj => {
    let title = obj.title
    pm.test(`${title} exists`, () => {
        let search = response.items.find(obj => typeof obj.title == 'string' && obj.title.indexOf(title) > -1);
        console.log(search);
        pm.expect(search, 'title search failed').to.not.be.undefined
     })
});

```

---

<div class="post-metadata">

**Author:** ![wilq8484](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@wilq8484](https://community.postman.com/u/wilq8484)\
**Post date:** [June 6, 2024, 12:55pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/28 "2024-06-06T12:55:46Z")

</div>

Nominated to responder of the week and month 🙂 many thanks

---

<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:** [July 6, 2024, 12:56pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/29 "2024-07-06T12:56:17Z")

</div>

Closed on July 6, 2024, 12:56pm UTC

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

[Previous page](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774.md?page=1)
