Comparing API response to JSON in an external file

Hello,

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?

@john-paul,

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.

Hi @dbraus , can you elaborate more with example how you did it?

Hi @dbraus please explain in the detail how we should do the comparison. I need very urgently and didnā€™t find the solution anywhere else.
@allenheltondev

@shreya4419 are you using a json file as iteration data and does that file also include expected schema for the response?

yesā€¦I am getting total 124 objects in my response body(postman) and also the external Json file contains the data in the same format (expected response as total 124 objects).
There is one ID field in each object and I want to check whether the data in ID field is same both place

I am using GET method for my api
@allen.helton