# AssertionError: expected undefined to be a string - getting this error

**URL:** <https://community.postman.com/t/assertionerror-expected-undefined-to-be-a-string-getting-this-error/49060>\
**Category:** Help Hub\
**Created:** [June 29, 2023, 11:13am UTC](https://community.postman.com/t/assertionerror-expected-undefined-to-be-a-string-getting-this-error/49060 "2023-06-29T11:13:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rk8420](https://avatars.discourse-cdn.com/v4/letter/r/f17d59/32.png) [@rk8420](https://community.postman.com/u/rk8420)\
**Post date:** [June 29, 2023, 11:13am UTC](https://community.postman.com/t/assertionerror-expected-undefined-to-be-a-string-getting-this-error/49060/1 "2023-06-29T11:13:00Z")

</div>

[  
{  
“id”: “1”,  
“name”: “John”,  
“location”: “india”,  
“phone”: “1234567890”,  
“courses”: [  
“java”,  
“selenium”  
]  
},

pm.test(“Testdata validation” , () =\>  
{  
const jsonData = pm.response.json();  
pm.expect(jdata).to.be.an(“object”);  
pm.expect(jsonData.location).to.be.a(“string”);

});

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 29, 2023, 12:13pm UTC](https://community.postman.com/t/assertionerror-expected-undefined-to-be-a-string-getting-this-error/49060/2 "2023-06-29T12:13:13Z")

</div>

Your response is an array with a single object in.

So you have to reference the array by its index. Array indexes start at zero.

```auto
pm.expect(jsonData[0].location).to.be.a(“string”);

```

---

<div class="post-metadata">

**Author:** ![rk8420](https://avatars.discourse-cdn.com/v4/letter/r/f17d59/32.png) [@rk8420](https://community.postman.com/u/rk8420)\
**Post date:** [June 29, 2023, 2:40pm UTC](https://community.postman.com/t/assertionerror-expected-undefined-to-be-a-string-getting-this-error/49060/3 "2023-06-29T14:40:19Z")

</div>

thanks it worked,

what about incase of multiple objects  
[  
{  
“id”: “1”,  
“name”: “John”,  
“location”: “india”,  
“phone”: “1234567890”,  
“courses”: [  
“java”,  
“selenium”  
]  
},  
{  
“name”: “Raunak”,  
“location”: “india”,  
“phone”: “98876543213”,  
“courses”: [  
“python”,  
“appium”  
],  
“id”: “2”  
},  
{  
“id”: “3”,  
“name”: “Smith”,  
“location”: “Canada”,  
“phone”: “165498765”,  
“courses”: [  
“C#”,  
“RestAPI”  
]  
}  
]

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 29, 2023, 5:12pm UTC](https://community.postman.com/t/assertionerror-expected-undefined-to-be-a-string-getting-this-error/49060/4 "2023-06-29T17:12:07Z")

</div>

It’s all according to what you want to test.

You may have to loop though the response like in the following example.

```auto
const response = pm.response.json();
console.log(response);

response.forEach(object => {
    pm.test(`location for id${object.id} is a string`, () => {
        pm.expect(object.location).to.be.a("string");
    });
});

```

This is using a concept called Template Literals and in particular string interpolation to customize the test case name, otherwise you just get the same test case name for each object (which makes it hard to troubleshoot when it fails).

[Template literals (Template strings) - JavaScript | MDN (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)

The test case uses backticks, and you can include variables wrapped in ${}.

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