AssertionError on Json array

I have an API Endpoint that returns a JSON array in response:

[
    {
        "status": "On Loan",
        "author": "Bettoni",
        "barcode": "123455",
        "published": "America Press, 1961.",
        "title": "Duns Scotus:",
        "dueDate": "2017-05-31"
    }
]

I have a test script that asserts for keys using Chaijs expect styling for keys
For some reason the assertion fails with an error because its checking the keys on index 1 of the data array which is obviously null.
Why does this script run assertions on the array values that are null?

pm.test("Response body must have valid keys", function () {
  var data = JSON.parse(responseBody);
  pm.expect(data).to.have.all.keys('status', 'author', 'barcode', 'published', 'title', 'dueDate');
});
AssertionError: expected [ Array(1) ] to have keys 'status', 'author', 'barcode', 'published', 'title', and 'dueDate

If I change the assertion to just the value on index 0 of the array, it works. But I don’t want to just check 1 value in the array.

pm.expect(data[0]).to.have.all.keys('status', 'author', 'barcode', 'published', 'title', 'dueDate');

Hey @harsh.parekh, the assertion fails because the expect syntax doesn’t go deep into each array item for assertions, so in your case, you’re basically expecting the array to include the specified keys, which obviously fails.

If you do not want to assert on a specified index, you can simply loop through the array and assert on each item. This is how it would look.

pm.test("Response body must have valid keys", function () {
  var data = JSON.parse(responseBody);
  data.forEach(function(item) {
      pm.expect(item).to.have.all.keys('status', 'author', 'barcode', 
      'published', 'title', 'dueDate'); 
  });
});

Feel free to reach out in case you have additional queries or if this doesn’t solve your problem.

1 Like

Hello there! I am a bit late to this party but I was wondering if I can get any help for a similar issue I have.
Basically, I am sending a POST request to an API endpoint using a data file (with Newman).
In the POST script I call the variable file source {{src}} and an array {{file}} in the data.json.
The data.json file is:

[{
		"src": "/C:/Users/../Test/DOC Test.doc",
		"file": {
			"fileName": "Test.doc ",
			"fileReference": "{{$timestamp}}",
			"source": "USER ",
			"ipAddress": "127.0 .0 .1 ",
			"uploadedBy": "user"
			}
},

	{
		"src": "/C:/Users/.../Test/DOCX Test.docx",
		"file": {
			"fileName": "DOCX Test.docx ",
			"fileReference": "{{$timestamp}}",
			"source": "USER ",
			"ipAddress": "127.0 .0 .1 ",
			"uploadedBy": "user"
			}
	}
]

The fail reason is: expected 500 to be one of [ 201, 202 ] at assertion:0 in test-script.
I really don’t have a clue. I try to search online but without any success.
Can anybody help me on this?

Thanks.

Hi @andrea.paddeu,

Welcome to the Postman community! For you to access the file and the src contained in the JSON file from the Tests or Pre-requests tab, you would need to use the pm library as outlined here: