Comparing API response to JSON in an external file


In Postman, is there a way to compare the API response body to an external JSON file (which contains the expected response body)?

It is well documented on how to feed the request body from an external file, but I can’t find much on bringing in external data to be used outside the request body.

Right now, the only way I know how to do this comparison is to paste the expected response body in to the Pre-req or tests section. But I have hundreds of cases where I have

  1. the request body in one file
  2. the expected response body in a different file

How can I bring the JSON in from each file to compare the response body to the expected response body?

Hi @dbraus!

I don’t know of a way to do this offhand. Let me check with our team and see if anyone has any ideas. To confirm, you’d essentially be referencing two separate JSON files during a Collection run? Is it possible to get the 2nd JSON payload from a request, or does it have to be a local file?


Ideally the solution would entail referencing two separate JSON files during a collection run…however, if that’s not possible we could probably bring up a server to host the 2nd JSON payload (or both for that matter) so it could be accessed from a request…is this what you are meaning by the last question in your post? I’d prefer to do it from a local file though.

Right now I’m thinking maybe I could append the ‘expected response’ to the end of the response body in the same JSON file. Then for my tests I could call the responseBody as well as individual .data variables (from the section of the JSON that has the expected response).

I’m not sure if this would work, I’m working on testing it out. Right now, my biggest hang-up has to do with dealing with setting up the request body to happily accept nested JSON structures of varying array lengths from an external file…but I’ve created a separate thread for this:

I figured out a way to do this. I added the expected API response as a parameter to the external file I was using to feed the API request body. Then I assign the expected API response coming in from the external file to an environmental variable in the pre-req, then I can use that to write tests against. I used this method along with the method outlined in this thread.