How can i assert different values for one field for this same id


I have case when my response include JSON objects for this same id but with different subjects.
I would like to check if one of subject is equal to my value.

        "id": "12345",
        "subject": "xxx",
         "id": "12345",
        "subject": "xxz",
          "id": "12345",
        "subject": "zxy",

I would like to check if one of subject is equal to ‘zxy’ based on id.

pm.test('Check of subject', function(){
let jsonData = pm.response.json();
let id;
for(let filter of jsonData){

if( '12345') {
    id= filter;
    console.log(id.subject); // here i have all subjects in console
 var data = id.subject
    for (var i = 0; i < data.length ; i++)

test is fail because there is subject ‘xxx’ expected.
Could you please help ? I know that answer for this case is not a big deal but i tried a lot of things and still nothing :frowning:

Hey @Matjas_22,

I think I understand what you meant but apologies if this isn’t quite right:

pm.test('Check of subject', () => {
    let jsonData = pm.response.json(),
        subjects = [];
    _.each(jsonData, (item) => {
        if( === '12345') {

I did things slightly differently and pushed each of the subjects to a new array and then used the .include()to see if that value was part of the array.


Hi, @dannydainton
I tried with your solution, but what i see now is ‘expected ’ like a new array is empty.

I only based the code on the example response body that you provided, if the id in the if statement is not in the response, it would return an empty array.

You could add a message to the expect statement:

pm.expect(subjects).to.include("zxy", "Id not found in the response body"); 

You might be better off adding a condition if the array is empty and capture that a different way.

if(subjects.length === 0) {"No Subject in the Subjects Array")

Now it works,
I changed order for scripts, before I was performing your solution, after my script so maybe it was the reason why array was empty. Now everything is fine, and expected results are there.
Thanks a lot.

1 Like