# 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:** 20\
**Page:** 1

<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 3, 2024, 3:19pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/1 "2024-06-03T15:19:02Z")

</div>

I have some property to test in JSON response using test data of JSON file.

How can I validate that data from JSON test data file (only one value and iteration ‘title’) matches property under items.

I would like to access following property in response in other way than:  
`x.items[5].title`

For the moment I can easily reach it by hardcoding:

```auto
const jsonData = pm.response.json();
        
pm.expect(jsonData.items[5].title).to.eql(pm.iterationData.get("title"));

```

but how to use it without specifying this [5]. Is there other way to “say” find title within items no matter of positioning?

If I would have in JSON test file e.g. 100 titles to verify, then it will be easier than specifying same 100 lines of the code…

---

<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 4, 2024, 9:37am UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/2 "2024-06-04T09:37:20Z")

</div>

Postman uses JavaScript under the hood, so this can be achieved using the JavaScript find() or filter() functions.

If you post an example response with 2-3 objects, I can help you with the specific formatting for the function.

---

<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 4, 2024, 9:50am UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/3 "2024-06-04T09:50:31Z")

</div>

I can send you screenshot from example, JSON path finder. In fact there are 8 titles which I need to validate using some smart solution:

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

---

<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 4, 2024, 9:55am UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/4 "2024-06-04T09:55:41Z")

</div>

If you send the request, you can copy and paste from the response pane.

I need the request as text, as I send it to Postman Echo to replicate the response.

When you say eight titles. What test are you envisioning? Do you need to test all eight titles, or just the 1?

---

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

</div>

request is normal GET of specific ID of that collection of documents

I will try to mock response a bit, as that is sensitive data ☹

yes let’s say to simplify I have only one expected ‘title’ in JSON test data file and in test I want to find it easily in that list of 8 titles under items property.

However final thing would be to have test which will go thru all 8 expected titles in JSON test data file and validate it against JSON response, however order is not needed, just fact if some title exists in response or not

that is JSON test data:

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

```

---

<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 4, 2024, 10:42am UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/6 "2024-06-04T10:42:02Z")

</div>

Here is an example using find, and another example using a forEach loop with a basic test.

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

pm.test('Title5 exists', () => {
    let search = response.find(obj => obj.title === "Title5");
    pm.expect(search).to.not.be.undefined
})

response.forEach((obj, i) => {
    pm.test(`title exists - object ${i} `, () => {
        pm.expect("title" in obj).to.be.true
        pm.expect(obj.title).to.not.be.undefined
    })
});

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/d/0d53db2240822fb4fc16c68118166781dfe0c420.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 4, 2024, 12:03pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/7 "2024-06-04T12:03:44Z")

</div>

thanks, how can I use `pm.iterationData` here? instead of `get(“title”)’

I would like to compare values from JSON test data vs JSON api response

---

<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 4, 2024, 12:51pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/8 "2024-06-04T12:51:51Z")

</div>

Assign the iteration data to a variable.

```auto
let title = pm.iterationData.get("title");

pm.test(`${title} exists`, () => {
    let search = response.find(obj => obj.title === title);
    pm.expect(search).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 4, 2024, 1:02pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/9 "2024-06-04T13:02:30Z")

</div>

thanks, but I’m getting this error while executing collection with JSON test data:

```auto
TypeError: Cannot read properties of undefined (reading 'find')

```

here is my complete post-script:

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

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

pm.test(`${title} exists`, () => {
    let search = response.find(obj => obj.title === 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 4, 2024, 1:19pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/10 "2024-06-04T13:19:19Z")

</div>

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

```

You just need to remove the data element from the end.

It’s included because this is how Postman Echo works. I usually remove it when I post code, but forgot in this instance.

---

<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 4, 2024, 1:38pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/11 "2024-06-04T13:38:47Z")

</div>

ok, thanks it was indeed looking weirdly.

close to resolve issue 😉

now there is an error: `response.find is not a function`

---

<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 4, 2024, 1:54pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/12 "2024-06-04T13:54:52Z")

</div>

What does the response look like?

Is it the same as the example you gave me. (Which is an array of objects).

The error message is basically telling that you response is not an array.

find is a JavaScript function\method that is only available to arrays, so if response is not an array, then the find method will not exist, so its correctly telling you that find is not a function.

---

<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 4, 2024, 2:10pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/13 "2024-06-04T14:10:06Z")

</div>

Ouch, sorry for misunderstanding. In the example I send you test data which I’m uploading while running collection - and that is simply JSON file.

Response is also in JSON and I would need to look under this JSON path finder,example:  
`x.items[4].title`

so in my code I have been using that:  
`pm.expect(jsonData.items[4].title).to.eql(pm.iterationData.get("title"));`

which of course works for single element, but how to look (expect) for ‘title’ in any items property?

---

<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 4, 2024, 2:24pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/14 "2024-06-04T14:24:38Z")

</div>

It would appear that your array is under items, so the find function would need to be something like…

```auto
const response = pm.response.json();
let search = response.items.find(obj => obj.title === title);

```

---

<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 4, 2024, 2:30pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/15 "2024-06-04T14:30:44Z")

</div>

great, it works - thanks, a lot

now just need to make some loop and add rest of the titles into the JSON test data file, to validate whole JSON test data title against response. Can I reuse forEach function somehow?

---

<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 4, 2024, 2:56pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/16 "2024-06-04T14:56:45Z")

</div>

Not sure I follow you.

The JSON data files used with the collection runners need to be an array of objects, with each iteration using the data from a single object.

If you want to validate all of the objects in a response in a single request, then you will need to format your data file differently.

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

```

This is an array with a single object in it (so will only run one iteration). That object has a single key called testData where the value is your array of test data.

You can then access the testData array using…

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

Or the special data variable (which contains the current iterations data).

`let testData = data.testData`

---

<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 4, 2024, 5:08pm UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/17 "2024-06-04T17:08:07Z")

</div>

Thanks a lot for your effort, you helped me a lot

I will ping you one more time if there will be some error with executing it against modified json test data

---

<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:43am UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/18 "2024-06-05T06:43:50Z")

</div>

hello

btw do I need to use forEach as in your first example? or how it should look like?

---

<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, 8:52am UTC](https://community.postman.com/t/how-can-i-test-nested-properties-using-json-test-data/61774/19 "2024-06-05T08:52:10Z")

</div>

Depends on what you want to test.

It sounds like you want a forEach loop against the test data array, and then test to see if the current title is in the response, so yes, a forEach loop would help with this.

Define the current title in the loop as a variable, and you can then use backticks and string literals to customize the test case name, like following.

`pm.test(`${title} exists`, () => {`

Otherwise, all of the tests will have the same name, and it will be hard to work out which one has failed.

---

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

</div>

yes, I would like to simply test if all titles from JSON files exist in the API JSON response (within items in my example)

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