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!!