Postman array assertion

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”
}]
}]
}]

Hey @aerospace-geoscient7! Welcome to the community :clap:

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:

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!

1 Like

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

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.

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:

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!

Hi bbahadur This works very well, Thank you so much for helping me out on this. Will keep posting if I do have any clarification

1 Like

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)

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”);
})
})

Hey @aerospace-geoscient7 ,

If you want to access the collection variable,

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!