# Postman array assertion

**URL:** <https://community.postman.com/t/postman-array-assertion/29164>\
**Category:** Help Hub\
**Created:** [October 28, 2021, 2:28am UTC](https://community.postman.com/t/postman-array-assertion/29164 "2021-10-28T02:28:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aerospace-geoscient7](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@aerospace-geoscient7](https://community.postman.com/u/aerospace-geoscient7)\
**Post date:** [October 28, 2021, 2:28am UTC](https://community.postman.com/t/postman-array-assertion/29164/1 "2021-10-28T02:28:07Z")

</div>

My Question:

1. I want to assert “FP”: “FOP2”.
2. And If 1st step is success. I want to Assert “SMO”: false,
3. And also I need to Assert SP.name.FOP2 and its corresponding Exam.“Marksguid”: “f778dd51-d401-42ca-a5ea-4920db5708b2”,

[{  
“FP”: “DAV1”,  
“SMO”: false,  
“SP”: [{  
“name”: “FOP1”,  
“Exam”: [{  
“Marksguid”: “a02dac4f-630f-4ed0-aa54-8c7044b71f2f”,  
“apiUrl”: “test1”,  
“interval”: 65,  
“schemaVersion”: “1.2”,  
“authenticationType”: “Basic”  
}]  
}]  
}, {  
“FP”: “FOP10”,  
“SMO”: false,  
“SP”: [{  
“name”: “FOP10”,  
“Exam”: [{  
“Marksguid”: “f778dd51-d401-42ca-a5ea-4920db5708b2”,  
“apiUrl”: “test1”,  
“interval”: 65,  
“schemaVersion”: “1.2”,  
“authenticationType”: “Basic”  
}]  
}]  
}, {  
“FP”: “FOP2”,  
“SMO”: false,  
“SP”: [{  
“name”: “FOP2”,  
“Exam”: [{  
“Marksguid”: “8c49f158-7286-4f15-904c-564b41966b6b”,  
“apiUrl”: “test1”,  
“interval”: 65,  
“schemaVersion”: “1.2”,  
“authenticationType”: “Basic”  
}]  
}]  
}, {  
“FP”: “FOP3”,  
“SMO”: false,  
“SP”: [{  
“name”: “FOP3”,  
“Exam”: [{  
“Marksguid”: “c5f2893f-b8fb-4494-8126-b3de47ebe440”,  
“apiUrl”: “test1”,  
“interval”: 65,  
“schemaVersion”: “1.2”,  
“authenticationType”: “Basic”  
}]  
}]  
}]

---

<div class="post-metadata">

**Author:** ![bbahadur](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@bbahadur](https://community.postman.com/u/bbahadur)\
**Post date:** [October 28, 2021, 8:38am UTC](https://community.postman.com/t/postman-array-assertion/29164/2 "2021-10-28T08:38:49Z")

</div>

Hey @aerospace-geoscient7! Welcome to the community 👏

Usually, to do assertions, we save the response and extract specific data and perform assertions on it. So, to solve your requirements, we can save the response, loop through each object and perform the required assertions.  
The following code will meet your 1st and 2nd requirement:

```auto
var jsonData=pm.response.json();

pm.test('Assertions', () => {
    _.each(jsonData, (item) => {      
        if(item.FP=="FOP2")
        pm.expect(item.SMO).to.eql(false)     
    })
})

```

Similarly, you can implement the 3rd requirement as well!

Good luck!

---

<div class="post-metadata">

**Author:** ![aerospace-geoscient7](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@aerospace-geoscient7](https://community.postman.com/u/aerospace-geoscient7)\
**Post date:** [October 28, 2021, 5:37pm UTC](https://community.postman.com/t/postman-array-assertion/29164/3 "2021-10-28T17:37:58Z")

</div>

Thank you so much it works well. I Was able to close it. Is there a way to connect MY Sql from postman. I want to connect to MYSQL and get an data from the table based on that I need do some operation on the postman!!

---

<div class="post-metadata">

**Author:** ![aerospace-geoscient7](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@aerospace-geoscient7](https://community.postman.com/u/aerospace-geoscient7)\
**Post date:** [October 29, 2021, 1:44am UTC](https://community.postman.com/t/postman-array-assertion/29164/4 "2021-10-29T01:44:18Z")

</div>

I facing one issue, this assertion is not failing when I give if(item.FP==“testnorecords”) condition which is not there in the response array. Please help me how to resolve this issue? I want this test to be failed when I give the value that doesn’t exist in the response.

---

<div class="post-metadata">

**Author:** ![bbahadur](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@bbahadur](https://community.postman.com/u/bbahadur)\
**Post date:** [October 29, 2021, 7:54am UTC](https://community.postman.com/t/postman-array-assertion/29164/5 "2021-10-29T07:54:27Z")

</div>

Hello!

So basically, what you want to do is Check if a particular value for FP exists or not.  
So to do this, you can store the required value “testnorecords” in a variable and check if it is there in the response. One way to do it is:

```auto
pm.test('Assertions', () => {
var FP_exists; // a variable in which we will save the required FP from response if it exists
_.each(jsonData, (item) => {
if(item.FP=="FOP2")
pm.expect(item.SMO).to.eql(false)

if(item.FP=="testnorecords")
 FP_exists=item.FP;
})
//after looping through all objets, we check if the required FP value exists or not
pm.test("testnorecords exists",()=>{
pm.expect(FP_exists).to.eql("testnorecords");
})
})

```

Hope this helps,  
Good luck!

---

<div class="post-metadata">

**Author:** ![aerospace-geoscient7](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@aerospace-geoscient7](https://community.postman.com/u/aerospace-geoscient7)\
**Post date:** [November 3, 2021, 2:55am UTC](https://community.postman.com/t/postman-array-assertion/29164/6 "2021-11-03T02:55:27Z")

</div>

Hi [bbahadur](https://community.postman.com/u/bbahadur) This works very well, Thank you so much for helping me out on this. Will keep posting if I do have any clarification

---

<div class="post-metadata">

**Author:** ![aerospace-geoscient7](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@aerospace-geoscient7](https://community.postman.com/u/aerospace-geoscient7)\
**Post date:** [November 8, 2021, 6:13pm UTC](https://community.postman.com/t/postman-array-assertion/29164/7 "2021-11-08T18:13:21Z")

</div>

Hi I do have one clarification

What if I’m comparing if(item.FP) with the collection variable (if FOP2 (value) is in a collection variable)  
// Need to verify the below as if (item.FP == get(collectionvariable). Help me on this  
if(item.FP==“FOP2”)  
pm.expect(item.SMO).to.eql(false)

---

<div class="post-metadata">

**Author:** ![aerospace-geoscient7](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@aerospace-geoscient7](https://community.postman.com/u/aerospace-geoscient7)\
**Post date:** [November 9, 2021, 5:59am UTC](https://community.postman.com/t/postman-array-assertion/29164/8 "2021-11-09T05:59:11Z")

</div>

> [@aerospace-geoscient7](#):
>
> if I’m comparing if(item.FP) with the collection variable (if FOP2 (value) is in a collection variable)

The below Script is working fine.  
Now if I’m comparing if(item.FP) with the collection variable (if FOP2 (value) is in a collection variable).  
I ahd FOP2 value in a collection variable now I want to compare if(item.FP==Collectionvariable) I used get collection variable syntax but still it throws undefined

pm.test(‘Assertions’, () =\> {  
var FP\_exists; // a variable in which we will save the required FP from response if it exists  
\_.each(jsonData, (item) =\> {  
if(item.FP==“FOP2”)  
pm.expect(item.SMO).to.eql(false)

if(item.FP==“testnorecords”)  
FP\_exists=item.FP;  
})  
//after looping through all objets, we check if the required FP value exists or not  
pm.test(“testnorecords exists”,()=\>{  
pm.expect(FP\_exists).to.eql(“testnorecords”);  
})  
})

---

<div class="post-metadata">

**Author:** ![bbahadur](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@bbahadur](https://community.postman.com/u/bbahadur)\
**Post date:** [November 9, 2021, 9:54am UTC](https://community.postman.com/t/postman-array-assertion/29164/9 "2021-11-09T09:54:19Z")

</div>

Hey @aerospace-geoscient7 ,

If you want to access the collection variable,

```auto
pm.collectionVariables.set("Collection_Variable","FOP2");
//You can also set it from the variables tab in the collection folder

var collection_var_value= pm.collectionVariables.get("Collection_Variable");
//accessing the variable and storing it.
//Using collection_var_value, you can perfor your tests.
console.log(collection_var_value);

```

If the above doesn’t work,  
1.Make sure you have properly set the Collection Variables.  
2.Ensure that the get method has the exact name of the collection Variable.

Good luck!
