# Comparing API response to JSON in an external file

**URL:** <https://community.postman.com/t/comparing-api-response-to-json-in-an-external-file/11490>\
**Category:** Help Hub\
**Created:** [March 23, 2020, 5:50pm UTC](https://community.postman.com/t/comparing-api-response-to-json-in-an-external-file/11490 "2020-03-23T17:50:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbraus](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@dbraus](https://community.postman.com/u/dbraus)\
**Post date:** [March 23, 2020, 5:50pm UTC](https://community.postman.com/t/comparing-api-response-to-json-in-an-external-file/11490/1 "2020-03-23T17:50:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![john-paul](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/john-paul/32/919_2.png) [@john-paul](https://community.postman.com/u/john-paul)\
**Post date:** [March 23, 2020, 9:53pm UTC](https://community.postman.com/t/comparing-api-response-to-json-in-an-external-file/11490/2 "2020-03-23T21:53:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dbraus](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@dbraus](https://community.postman.com/u/dbraus)\
**Post date:** [March 23, 2020, 10:04pm UTC](https://community.postman.com/t/comparing-api-response-to-json-in-an-external-file/11490/3 "2020-03-23T22:04:08Z")

</div>

@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:

> [@How to deal with input JSON with varying array lengths when reading from external file](https://community.postman.com/t/how-to-deal-with-input-json-with-varying-array-lengths-when-reading-from-external-file/11491):
>
> I have a situation where I’m sending JSON input payloads to my API that have arrays whose lengths can vary. I am wondering how to best deal with this when using an external file to populating my request body. I put a couple snippets of example request bodies - but basically, the “components” , “faultCodes” , “sensors” , “measurements” arrays can all be of varying length (all of which are valid inputs). So if I want to pull these request bodies in from an external file and have postman iterate…

---

<div class="post-metadata">

**Author:** ![dbraus](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@dbraus](https://community.postman.com/u/dbraus)\
**Post date:** [March 25, 2020, 7:52pm UTC](https://community.postman.com/t/comparing-api-response-to-json-in-an-external-file/11490/4 "2020-03-25T19:52:47Z")

</div>

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.

> [@Comparing API response to JSON in an external file](https://community.postman.com/t/comparing-api-response-to-json-in-an-external-file/11490):
>
> 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 the request body i…

---

<div class="post-metadata">

**Author:** ![VPanchal](https://avatars.discourse-cdn.com/v4/letter/v/5f9b8f/32.png) [@VPanchal](https://community.postman.com/u/VPanchal)\
**Post date:** [February 14, 2022, 6:27pm UTC](https://community.postman.com/t/comparing-api-response-to-json-in-an-external-file/11490/5 "2022-02-14T18:27:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shreya4419](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@shreya4419](https://community.postman.com/u/shreya4419)\
**Post date:** [September 14, 2022, 10:05am UTC](https://community.postman.com/t/comparing-api-response-to-json-in-an-external-file/11490/6 "2022-09-14T10:05:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 14, 2022, 11:58am UTC](https://community.postman.com/t/comparing-api-response-to-json-in-an-external-file/11490/7 "2022-09-14T11:58:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shreya4419](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@shreya4419](https://community.postman.com/u/shreya4419)\
**Post date:** [September 15, 2022, 4:57pm UTC](https://community.postman.com/t/comparing-api-response-to-json-in-an-external-file/11490/8 "2022-09-15T16:57:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [December 12, 2024, 12:52pm UTC](https://community.postman.com/t/comparing-api-response-to-json-in-an-external-file/11490/9 "2024-12-12T12:52:19Z")

</div>

Closed on December 12, 2024, 12:52pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
