How can I test nested properties using JSON test data?

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).

1 Like

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?

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
})```

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.

Based on the following response.

{
    "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.

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

Thanks again, will check it once will be back

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 :wink:

Taken from here…

Can I use wildcards when searching an array of strings in Javascript? - Stack Overflow

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
     })
});
1 Like

Nominated to responder of the week and month :slight_smile: many thanks

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