# Parsing JSON in Tests but cannot read property of undefined

**URL:** <https://community.postman.com/t/parsing-json-in-tests-but-cannot-read-property-of-undefined/26081>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [July 22, 2021, 7:50pm UTC](https://community.postman.com/t/parsing-json-in-tests-but-cannot-read-property-of-undefined/26081 "2021-07-22T19:50:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kristopher.perez](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@Kristopher.perez](https://community.postman.com/u/Kristopher.perez)\
**Post date:** [July 22, 2021, 7:50pm UTC](https://community.postman.com/t/parsing-json-in-tests-but-cannot-read-property-of-undefined/26081/1 "2021-07-22T19:50:38Z")

</div>

_My question_:  
I am using the following to help me iterate through some JSON data

```auto
   let response = pm.response.json();
    response.forEach(function(value){
        console.info((value.name)+","+(value.id)+","+(value.schema.type))
        }
    )

```

The data that I am working with looks this this:

```auto
{
       "id": "statuscategorychangedate",
       "key": "statuscategorychangedate",
       "name": "Status Category Changed",
       "custom": false,
       "orderable": false,
       "navigable": true,
       "searchable": true,
       "clauseNames": [
           "statusCategoryChangedDate"
       ],
       "schema": {
           "type": "datetime",
           "system": "statuscategorychangedate"
       }
   },
   {
       "id": "issuetype",
       "key": "issuetype",
       "name": "Issue Type",
       "custom": false,
       "orderable": true,
       "navigable": true,
       "searchable": true,
       "clauseNames": [
           "issuetype",
           "type"
       ],
       "schema": {
           "type": "issuetype",
           "system": "issuetype"
       }
   },
   {
       "id": "parent",
       "key": "parent",
       "name": "Parent",
       "custom": false,
       "orderable": false,
       "navigable": true,
       "searchable": false,
       "clauseNames": [
           "parent"
       ]
   },
...

```

The problem is when it iterates through, I get the first 2 values as expected, but the third value throws an error because schema doesn’t exist for that entry.

_Details (like screenshots)_:

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

---

<div class="post-metadata">

**Author:** ![stevejonn](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@stevejonn](https://community.postman.com/u/stevejonn)\
**Post date:** [April 24, 2023, 7:00am UTC](https://community.postman.com/t/parsing-json-in-tests-but-cannot-read-property-of-undefined/26081/2 "2023-04-24T07:00:49Z")

</div>

In JavaScript, nearly everything is an object except for null and undefined. The error “[Cannot read property of undefined](https://net-informations.com/js/iq/unerror.htm)” occurs when a property is read or a function is called on an undefined variable. Undefined indicates that a variable has been declared but not assigned a value. In JavaScript, properties and functions can only belong to objects. Since undefined is not an object type, calling a function or property on such a variable causes the TypeError: Cannot read property of undefined.

To prevent errors caused by variables that may not have a guaranteed value, it is recommended to verify their value for null or undefined before utilizing them. To ensure that the variables being accessed contain the expected value, there are several approaches that can be employed. One such approach is to perform if checks prior to working with objects that may undergo changes:

```auto
if (myVar !== undefined) {
    ...
}

Or

if (typeof(myVar) !== 'undefined') {
    ...
}

```
