Checking for existence of elements in Json Array

Iā€™m trying to confirm if an element in an array exists.

This relates to some CosmoDB and you can see an example of the JSON that gets returned at the following link.

I think its a JSON object within a JSON array, just to make it a bit more complex.

A database can have multiple collections (not to be confused with Postman collections).

I want to check if a particular collection exists.

So in the Microsoft example. How would I for example check if a collection existed with the ID of ā€œSampleCollectionā€.

Pretty sure its just my javascript experience letting me down. Iā€™ve tried various combinations of indexof and find.

If I know what the array element is for the collection I can just put.

pm.test(ā€˜Assessments collection existsā€™, () => {
pm.expect(response.DocumentCollections[0].id).to.eql(ā€˜SampleCollectionā€™);
});

Can you search responses using JSONPath (or XPath for that matter). These are simple queries for either of those.

Iā€™m getting somewhere.

I can at least get to the element, although not sure if this is the best way.

console.log(response.DocumentCollections.find(obj => {return obj.id === ā€˜SampleCollectionā€™}))

I tried the followingā€¦

pm.expect((response.DocumentCollections.find(obj => {return obj.id === ā€˜SampleCollectionā€™})).to.have.property(ā€˜idā€™));

but get the following error.

TypeError: Cannot read properties of undefined (reading ā€˜haveā€™)

Not sure which assertion I should be using. I guess all I really want to check if that is null or not. I know that ID must exist or the find query wouldnā€™t work.

Sorry final post for now. The following seems to work.

pm.test(ā€˜Collection Searchā€™, () => {
let collectionSearch = (response.DocumentCollections.find(obj => {return obj.id === ā€˜SampleCollectionā€™}));
pm.expect(collectionSearch).to.have.property(ā€˜idā€™);
pm.expect(collectionSearch).to.be.an(ā€˜objectā€™);
});

Appreciate comments on the approach, or if there is a better\more efficient way of doing this type of test.

1 Like

Hi @michaelderekjones

Iā€™m curious to know more about this approach. Iā€™m new to JavaScript and just starting to get my head around arrow functions etc.

What is the benefit of using a function over just accessing the property using dot notation?


For example;

const myResponse = pm.response.json();

pm.test('Collection Search', () => {
    pm.expect(myResponse.DocumentCollections[0]).to.have.property('id');
    pm.expect(myResponse.DocumentCollections[0]).to.be.an('object');
});

@w4dd325

I donā€™t think Iā€™ve written or explained the test very well.

What I actually wanted to do is ensure that the value ā€œSampleCollectionā€ was in the results for id.

There are multiple collections in the response. What you have will pass, but its not confirming that SampleCollection exists. (Cosmos DB collection, not to be confused with Postman collections).

Iā€™ve only just started using Postman, and Iā€™m used to jsonpath and xpath assertions in tools like SoapUI. I know a bit about Javascript but havenā€™t used it in anger. So just learning how to dig into the responses, attributes, etc at the moment.

Iā€™ve done this in a slightly better way since. Which is to get all IDā€™s, and then check what Iā€™m looking for is in the response.


pm.test('Collection Search v2', () => {
    var IDs = response.DocumentCollections.map(DocumentCollections => DocumentCollections.id);
    // console.log(IDs);
    pm.expect(IDs).to.be.an('array');
    pm.expect(IDs).to.include('SampleCollection');
});

A couple more ways to get all of the IDā€™s.

// another way of getting alls the ID's.
var mapTest1 = response.DocumentCollections.map(function(item){return item.id;});
// and another
var mapTest2 = response.DocumentCollections.map(({ id }) => id);
1 Like

Thanks for that, I think I get it.
Itā€™s an interesting approach and looks like a good alternative to some of the things I have done in previous tests!!