1

Can somebody please help me to assert array in array in postman ?

I have this peace of code:

"documents": [
        {
            "fileName": "file01_guid.pdf",
            "documentType": "document",
            "parentFiles": [
                "file01.pdf"
            ]
        },
        {
            "fileName": "file02_guid.pdf",
            "documentType": "document",
            "parentFiles": [
                "file01.pdf"
            ]
        }

I need to assert the "ParentFiles" array using this method:

var array = [];
var range = (json_response.data.documents).length

for (var i = 0; i < range; i++)

{
 var file = json_response.data.documents[i].fileName
 var type = json_response.data.documents[i].documentType;

 array.push(file)
 array.push(type)
}

So I could write this kind of test:

{
    pm.expect(array).to.include("file01.pdf", "file01.pdf");
});

Thank you in advance

5
  • what you want to assert ? why are you pushind document type also ? Commented Mar 10, 2021 at 13:28
  • 2
    Why is “Can someone help me?” not an actual question? Commented Mar 10, 2021 at 13:29
  • I want to assert the parentFiles array using "include" function, like I've described it in my post. I'm stuck a little Commented Mar 10, 2021 at 13:34
  • you want to assert parentFile array of each object or you want to assert the new array with some array Commented Mar 10, 2021 at 13:35
  • If there are two ways on how to approach my goal then - I want to know both. Commented Mar 10, 2021 at 13:36

2 Answers 2

1

You can verify if "file01.pdf" exists by simply filtering and checking the length. You can also build your array more efficiently by reducing.

const json_response = {
  "data": {
    "documents": [{
      "fileName": "file01_guid.pdf",
      "documentType": "document",
      "parentFiles": ["file01.pdf"]
    }, {
      "fileName": "file02_guid.pdf",
      "documentType": "document",
      "parentFiles": ["file01.pdf"]
    }]
  }
};

const array = json_response.data.documents.reduce((acc, doc) => {
  const { fileName, documentType, parentFiles: [ pfName ] } = doc;
  return [ ...acc, fileName, documentType, pfName ];
}, []);

console.log(array);

// Exactly two instances of "file01.pdf" exist.
console.log(array.filter(val => val === "file01.pdf").length === 2);
.as-console-wrapper { top: 0; max-height: 100% !important; }

If you just want to compare the arrays, you can try the following:

const json_response = {
  "data": {
    "documents": [{
      "fileName": "file01_guid.pdf",
      "documentType": "document",
      "parentFiles": ["file01.pdf"]
    }, {
      "fileName": "file02_guid.pdf",
      "documentType": "document",
      "parentFiles": ["file01.pdf"]
    }]
  }
};

const allEqual = json_response.data.documents
  .every(({ parentFiles: [ filename ] }) => filename === "file01.pdf");

console.log(allEqual);
.as-console-wrapper { top: 0; max-height: 100% !important; }

Sign up to request clarification or add additional context in comments.

4 Comments

why can't you just use lodash ?
@PDHide because JavaScript has evolved and dependencies are extra overhead.
@PDHide The code works in NodeJS too, it is not DOM-manipulation (or dependent) code.
@Mr.Polywhirl In the end I've decided to use your method: const array = json_response.data.documents.reduce((acc, doc) => { const { fileName, documentType, parentFiles: [ pfName ] } = doc; return [ ...acc, fileName, documentType, pfName ]; }, []); Because I the response, the documents can be different. I've studied your method and it totally fits to my needs. Thank you
0

you can loop using foreach and compare the values

//store expected files in order
array=["file01.pdf","file01.pdf"]

//now forEach on each element
json_response.data.documents.forEach( (elem,index,arr)=>{
pm.expect(elem.parentFiles[0]).to.be.eql(array[index])
})

If you just want to compare two arrays then:

pm.expect(_.isEqual(array,array2)).to.be.true

lodash isEqual can be used to compare two object like arrays

2 Comments

I have not understood your "loop" option. When I've tried: pm.test('parentFiles equal "BR_21080_file18.pdf"', function() { array=["file01.pdf","file01.pdf"] pm.response.json().documents.forEach( (elem,index,arr)=>{ pm.expect(elem.parentFiles[0]).to.be.eql(array[index]) })}); I've received "parentFiles equal "file18.pdf" | TypeError: Cannot read property 'forEach' of undefined"
is the updated code json_response.data.documents

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.