# How to extract response body elements full branch and verify same data displays in another api (GET)

**URL:** <https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [August 6, 2020, 4:35pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788 "2020-08-06T16:35:30Z")\
**Posts on this page:** 19\
**Page:** 2

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [August 17, 2020, 10:10pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/21 "2020-08-17T22:10:16Z")

</div>

Hi @shahbaz.shiekhqa,

So you just need to use the proper attribute for err. First, I would try console logging the whole error, that way, you can then see what attributes it has.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/9/d9349ecbec6b306a83205285a670ec2cf84e8370.png)

So now, if you want to print something else out, (say hostname), you can do:

`console.log("Error is " + err.hostname);`

Best,  
Orest

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 17, 2020, 10:27pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/22 "2020-08-17T22:27:39Z")

</div>

> [@odanylewycz](#):
>
> So now, if you want to print something else out, (say hostname), you can do:
> 
> `console.log("Error is " + err.hostname);`

Thanks, i noticed when i changed that url into a text, and added it to the variable, when i print it to the console i am seeing undefined , and when i am using that same variable for my pm.sendrequest i am seeing that its not getting the url instead its just writing the variable as a url.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/8/985663b4a45ac75528d227bbb62cd16aead3e83d.png)

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [August 17, 2020, 11:47pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/23 "2020-08-17T23:47:54Z")

</div>

Hi @shahbaz.shiekhqa,

So in order to use a postman variable, you need to first obtain a reference to the variable. You can do that by using the following syntax in your script:

`var TracerswithAMPurl = pm.environment.get("TracerswithAMPurl");`

Then you can use the variable as you normally would with javascript.

```
pm.sendRequest(TracerswithAMPrul, function (err, response) {
    if (err) {
         console.log(err);
    }
   console.log("Response is " + pm.response.text());
   pm.expect.pass("Pass test")
});
```

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 18, 2020, 1:28pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/24 "2020-08-18T13:28:16Z")

</div>

Hi Thanks for that,

I do see the variable has turned into the url … added  
this  
var TracerswithAMPurl = pm.environment.get(“TracerswithAMPurl”);

and then  
pm.sendRequest(TracerswithAMPurl, function (err,response){

```
if (err){

    console.log(err);

    // pm.expect.fail()

}else{

console.log(response.json());

// pm.expect.pass("passed test")

}

```

});

but am seeing the same unexpected token issue …  
Json error …  
the url is a text so is that why its sending this error …? do you we need to convert that text into json ?

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/9/9210822b507c725191fa91f5a20dfd8ef956f124.png)

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [August 18, 2020, 1:51pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/25 "2020-08-18T13:51:48Z")

</div>

Hi @shahbaz.shiekhqa,

Thanks for providing the information up front.

From the looks of it, you error is coming from `response.json`. This is because your response actually isnt json, its just text. The snippet above was taken from something else that has a json response.

Instead, use `pm.response.text()` and you shouldnt have an error anymore. I’ve also updated the example above.

Thanks,  
Orest

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 18, 2020, 3:03pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/26 "2020-08-18T15:03:30Z")

</div>

> [@odanylewycz](#):
>
> `e.text()` and you shouldnt have an error anymore. I’ve also up

Thank you very much for that… instead of using the console.log , how can i show these results through the test results? i tried pm.expect.pass or pm.expect.fail but pass is not a function

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 18, 2020, 3:41pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/27 "2020-08-18T15:41:28Z")

</div>

Found the answer

thank you very much for all your help,

is it fine if i ask more questions on this forum …?

```auto
pm.test("Tracers with AMP Url hits the server successfully", function () {
    pm.sendRequest(TracerswithAMPurl, function (err, response) {
        pm.expect(err).to.not.be.ok;
        pm.expect(response).to.have.property('code', 200);
        pm.expect(response).to.have.property('status', 'OK');
    });
});

```

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [August 18, 2020, 4:42pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/28 "2020-08-18T16:42:33Z")

</div>

Hi @shahbaz.shiekhqa,

You’re welcome! Glad we can help and that you can figure it out.

You’re script looks pretty good.

As for asking more questions, I would say if you have new unrelated questions to the original issue for this post, I would make a new post around it, for organizational purposes.

Best,  
Orest

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 18, 2020, 7:19pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/29 "2020-08-18T19:19:43Z")

</div>

Hi I did post another question over here… this is a bit complex …

> [@How to verify full body response within a Pre requisite script and compare with response body?](https://community.postman.com/t/how-to-verify-two-elements-in-the-response-body-and-ensure-all-the-other-elements-display/15123):
>
> Hi , I have 67 branches below and i have added a variable within Pre - Requisite script var expectedJsonBody = [ { "siteId": 685, "siteName": "Saint Francis Hospital-Memphis", "hcoid": 2659, "defaultSite": false, "address": { "address1": "5959 Park Avenue", "address2": null, "city": "Memphis", "state": "TN", "zip": "38119-5198" } }, { pm.environment.set(“ExpectedSitesTodisplay”, JSON.stringify(expectedJsonBody)); …

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [August 20, 2020, 1:51pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/30 "2020-08-20T13:51:39Z")

</div>

Hi would you happen to know the answer to this

# [How to verify full body response within a Pre requisite script and compare with response body?](https://community.postman.com/t/how-to-verify-full-body-response-within-a-pre-requisite-script-and-compare-with-response-body/15123)

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [September 8, 2020, 3:59am UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/31 "2020-09-08T03:59:09Z")

</div>

Hi Allen,

I did the same test for another API, and i am getting it to pass, but not showing in the test results is there something wrong i am doing?

API 1:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/a/7ad2443c73960318ceb32c1af06beff6f4e2a6ee.png)

API2:  
const apiOne = JSON.parse(pm.collectionVariables.get(‘AlertBanner\_Message’));

const apiTwo = pm.response.json();

for(let i = 0; i \< apiOne.length; i++) {

```
const alertbanner = apiOne[i];    

const apiTwo = apiTwo.find(portalannouncement => portalannouncement.announcementId === alertbanner.announcementId && portalannouncement.header === alertbanner.header &&

 portalannouncement.body === alertbanner.body);

```

pm.test(`Has matching header for ${alertbanner.header}`, function (){

```
pm.expect(apiTwo).to.not.be.undefined;

});

```

Its not showing the test results of “Has matching header” Test  
just these two are showing:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/4/a4ea563c541aefd4ead11742fc2c70a1e813249b.png)

---

<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 8, 2020, 11:31am UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/32 "2020-09-08T11:31:48Z")

</div>

Can you show us the entire test script either in a screenshot or copy/paste the source please?

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [September 8, 2020, 7:54pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/33 "2020-09-08T19:54:55Z")

</div>

Sure,

this is the entire test script:

pm.test(“if 200 - Pass else Fail - 404 error”, function(){

```
pm.response.to.have.status(200);

```

});

pm.test(“Response Time is less than 600ms”, function(){

pm.expect(pm.response.responseTime).to.be.below(600);

});

const apiOne = JSON.parse(pm.collectionVariables.get(‘AlertBanner\_Message’));

const apiTwo = pm.response.json();

for(let i = 0; i \< apiOne.length; i++) {

```
const alertbanner = apiOne[i];    

const apiTwo = apiTwo.find(portalannouncement => portalannouncement.announcementId === alertbanner.announcementId && portalannouncement.header === alertbanner.header &&

 portalannouncement.body === alertbanner.body);

```

pm.test(`Has matching header for ${alertbanner.header}`, function (){

```
pm.expect(apiTwo).to.not.be.undefined;

});

```

}

---

<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 8, 2020, 8:56pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/34 "2020-09-08T20:56:51Z")

</div>

I’m going to reformat your test script for better readability.

```javascript
pm.test(“if 200 - Pass else Fail - 404 error”, function(){
  pm.response.to.have.status(200);
});

pm.test(“Response Time is less than 600ms”, function(){
  pm.expect(pm.response.responseTime).to.be.below(600);
});

const apiOne = JSON.parse(pm.collectionVariables.get(‘AlertBanner_Message’));
const apiTwo = pm.response.json();

for(let i = 0; i < apiOne.length; i++) {
  const alertbanner = apiOne[i];    
  const apiTwo = apiTwo.find(portalannouncement => portalannouncement.announcementId === alertbanner.announcementId && portalannouncement.header === alertbanner.header &&
 portalannouncement.body === alertbanner.body);

  pm.test(Has matching header for ${alertbanner.header}, function (){
    pm.expect(apiTwo).to.not.be.undefined;
  });
}

```

Since you aren’t seeing any _matching header tests_, that means that you don’t have any objects in your apiOne array and your for loop has nothing to iterate over.

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [September 9, 2020, 10:01pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/35 "2020-09-09T22:01:37Z")

</div>

> [@allenheltondev](#):
>
> ou aren’t seeing any _matching header tests_ , that means that yo

Hi

so for the first API i have  
the header displaying as customer technical support hours for test

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/2/d273be177fb52271e9343d62212b853704d1368f.png)

and the second api I have the same header…  
but everytime i run the code it should see that 3 tests were run but is only seeing two tests run…

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/0/b089a40a69f710fd67a5df53246d3cd1d3ae008e.png)

---

<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 10, 2020, 11:33am UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/36 "2020-09-10T11:33:38Z")

</div>

It looks like your first request returns an object, not an array.

So your for loop has nothing to iterate over, meaning it won’t create that third test. you could try this:

```auto
pm.test(“if 200 - Pass else Fail - 404 error”, function(){
  pm.response.to.have.status(200);
});

pm.test(“Response Time is less than 600ms”, function(){
  pm.expect(pm.response.responseTime).to.be.below(600);
});

const apiOne = JSON.parse(pm.collectionVariables.get(‘AlertBanner_Message’));
const apiTwo = pm.response.json();
    
const match = apiTwo.find(portalannouncement => portalannouncement.announcementId === apiOne.announcementId && portalannouncement.header === apiOne.header &&
 portalannouncement.body === apiOne.body);

pm.test(Has matching header for ${apiOne.header}, function (){
  pm.expect(match).to.not.be.undefined;
});

```

---

<div class="post-metadata">

**Author:** ![shahbaz.shiekhqa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@shahbaz.shiekhqa](https://community.postman.com/u/shahbaz.shiekhqa)\
**Post date:** [September 10, 2020, 10:13pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/37 "2020-09-10T22:13:10Z")

</div>

Allen, sir you are awesome, that worked perfectly  
so all we had to do was get rid of the loop … but than how do we know if its an object or array ?

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/5/c5c1ad78580e91353909146aa28083c11f209340.png)

Also have another question, man so much testing to do – with APIs

> [@How to click on response body link (that keeps changing) and verify that Id = Global Admin exists within the link?](https://community.postman.com/t/how-to-click-on-response-body-link-that-keeps-changing-and-verify-that-id-global-admin-exists-within-the-link/15773):
>
> Hi, I have the following url below, but for my test i want to click on it and check if the url has Global Admin as a User Role. API1 = Click on the link (which is always changing after every send) and verify once you send the url - User Role = Global Admin … I want to be able to do this in the test scripts of the first api call … is this possible? I am already sending the request for that url and it is coming up as pass, but what would be next… postman.setEnvironmentVariable("U…

---

<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 10, 2020, 10:29pm UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/38 "2020-09-10T22:29:40Z")

</div>

I’m very happy to hear that worked for you!

To tell if something is an object or an array, look at the enclosing characters

```auto
{} //object
[] //array

```

---

<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:** [August 25, 2023, 9:57am UTC](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788/39 "2023-08-25T09:57:56Z")

</div>

Closed on August 25, 2023, 9:57am UTC

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

[Previous page](https://community.postman.com/t/how-to-extract-response-body-elements-full-branch-and-verify-same-data-displays-in-another-api-get/14788.md?page=1)
