# Checking JSON body in a pm function

**URL:** <https://community.postman.com/t/checking-json-body-in-a-pm-function/7669>\
**Category:** Help Hub\
**Created:** [September 9, 2019, 12:05pm UTC](https://community.postman.com/t/checking-json-body-in-a-pm-function/7669 "2019-09-09T12:05:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Coxjeffrey](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Coxjeffrey](https://community.postman.com/u/Coxjeffrey)\
**Post date:** [September 9, 2019, 12:05pm UTC](https://community.postman.com/t/checking-json-body-in-a-pm-function/7669/1 "2019-09-09T12:05:10Z")

</div>

Hello,

I’m trying to test the response of a JSON. I’m putting it in a function so that I can use this code in other requests.

The code I’m using is:

```
var response = JSON.parse(responseBody);
var array_Id = 0;
var afwz_typ = response.HolidayRightsResults[0].HolidayRights[array_Id].AFWZ_TYP;

pm.test("Rechten Afwz_type "+ afwz_typ + " are ok!", function() {
pm.response.HolidayRightsResults[0].HolidayRights[array_Id].AFWZ_TYP == 07;
pm.response.HolidayRightsResults[0].HolidayRights[array_Id].BGN_DAT == "2019-01-01T00:00:00";
});

```

When I run this I’m getting an `TypeError: Cannot read property '0' of undefined`.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 9, 2019, 5:07pm UTC](https://community.postman.com/t/checking-json-body-in-a-pm-function/7669/2 "2019-09-09T17:07:21Z")

</div>

Hey @Coxjeffrey,

Not sure what you’re trying to do within the function but I did see that the code you have, wasn’t quite right.

The assertion syntax should look something like this:

`pm.expect(HolidayRightsResults[0].HolidayRights[array_Id].AFWZ_TYP).to.equal(07);`

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [September 10, 2019, 3:54am UTC](https://community.postman.com/t/checking-json-body-in-a-pm-function/7669/3 "2019-09-10T03:54:41Z")

</div>

As @danny-dainton pointed out the mistake with your tests, you’ll need to fix that.

Also, the error that you’re receiving is because you’re calling  
`pm.response.HolidayRightsResults[0]`

Now, `pm.response` is a string and you haven’t parsed it as JSON yet, and you’re trying to access a property called `HolidayRightsResults` of this string, which has given you undefined, then further you try to access the 0th index of this undefined value which hence gave you the error `Cannot read property ‘0’ of undefined’.

What you actually wanted is the following:

```javascript
// Updated script here
var response = pm.response.json(),
  array_Id = 0,
  afwz_typ = response.HolidayRightsResults[0].HolidayRights[array_Id].AFWZ_TYP;

pm.test("Rechten Afwz_type "+ afwz_typ + " are ok!", function() {
  // Use the parsed JSON in your assertion and not `pm.response` directly
  pm.expect(response.HolidayRightsResults[0].HolidayRights[array_Id].AFWZ_TYP).to.equal(07);
  // ...
});

```
