# Checking for existence of elements in Json Array

**URL:** <https://community.postman.com/t/checking-for-existence-of-elements-in-json-array/38126>\
**Category:** Help Hub\
**Tags:** tests, json\
**Created:** [August 11, 2022, 2:02pm UTC](https://community.postman.com/t/checking-for-existence-of-elements-in-json-array/38126 "2022-08-11T14:02:32Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [August 11, 2022, 2:02pm UTC](https://community.postman.com/t/checking-for-existence-of-elements-in-json-array/38126/1 "2022-08-11T14:02:32Z")

</div>

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.

> **[List Collections - Azure Cosmos DB REST API](https://docs.microsoft.com/en-us/rest/api/cosmos-db/list-collections)**
>
> List collections REST API syntax. Request and response headers, body, status codes and examples.

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.

---

<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:** [August 11, 2022, 3:00pm UTC](https://community.postman.com/t/checking-for-existence-of-elements-in-json-array/38126/2 "2022-08-11T15:00:37Z")

</div>

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.

---

<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:** [August 11, 2022, 3:11pm UTC](https://community.postman.com/t/checking-for-existence-of-elements-in-json-array/38126/3 "2022-08-11T15:11:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [August 16, 2022, 7:25pm UTC](https://community.postman.com/t/checking-for-existence-of-elements-in-json-array/38126/4 "2022-08-16T19:25:50Z")

</div>

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;

```auto
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');
});

```

---

<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:** [August 17, 2022, 8:45am UTC](https://community.postman.com/t/checking-for-existence-of-elements-in-json-array/38126/5 "2022-08-17T08:45:59Z")

</div>

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

```auto

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.

```auto
// 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);

```

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [August 17, 2022, 9:05am UTC](https://community.postman.com/t/checking-for-existence-of-elements-in-json-array/38126/6 "2022-08-17T09:05:02Z")

</div>

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