Student Expert JSON Issue

The Script to validate JSON data type of data:

const jsonData = pm.response.json(); 
pm.test("Test data type of the response", () => { 
pm.expect(jsonData).to.be.an("object"); 
//pm.expect(jsonData.name).to.be.a("string"); 
//pm.expect(jsonData.id).to.be.a("number"); 
//pm.expect(jsonData.courses).to.be.an("array"); 
});

JSON Data:

{
"students":[
    {
        "id": 1,
        "name": "John",
        "location": "india",
        "phone": "1234567890",
        "courses": [
            "Java",
            "Selenium"
        ]
    },
    {
        "id": 2,
        "name": "Kim",
        "location": "US",
        "phone": "98876543213",
        "courses": [
            "Python",
            "Appium"
        ]
    },
    {
        "id": 3,
        "name": "Smith",
        "location": "Canada",
        "phone": "165498765",
        "courses": [
            "C#",
            "RestAPI"
        ]
    }
]
}

**Error : Test data type of the response | AssertionError: expected [ { id: 1, name: 'John', …(3) }, …(2) ] to be an object**

Please help to resolve this issue.

Hi @akhilarelli55

I copied your JSONdata and test code into a mock and it appears to work ok.

image

Currently, the only way I could replicate your issue was to remove the first part ( { "students": ) and the closing } bracket of the JSONdata so that it was an array, like this:

Can you double check your response / JSONdata and ensure that you don’t have any unsaved changes etc.

Hi James Wadley, Thank you for a quick help on this issue. As per your suggestion i tried removing ( { "students": ) and the closing } bracket of the JSONdata.

//************JSON data modified as per your suggestion:

[
{
“id”: 1,
“name”: “John”,
“location”: “india”,
“phone”: “1234567890”,
“courses”: [
“Java”,
“Selenium”
]
},
{
“id”: 2,
“name”: “Kim”,
“location”: “US”,
“phone”: “98876543213”,
“courses”: [
“Python”,
“Appium”
]
},
{
“id”: 3,
“name”: “Smith”,
“location”: “Canada”,
“phone”: “165498765”,
“courses”: [
“C#”,
“RestAPI”
]
}
]

I am getting issue in command prompt. please find the attachment of CMD and POSTMAN. Please help on this .


Can we see a screenshot of the actual response, just to double check that its correct.

Can you confirm if the tests are passing in Postman. Are they failing in Newman? Is that the issue?

The data looks fine. You can console log the type of a variable to aid in troubleshooting.

However, an array is a special type of object, so if you console log it, it will show as “object”. So maybe not so useful in this circumstance.

It all looks ok to me and the following tests are passing. (Based on your original response\JSON with the array).

const jsonData = pm.response.json();

console.log(jsonData);

pm.test("response to be an object", () => { 
    console.log(typeof(jsonData));
    pm.expect(jsonData).to.be.an("object");     
});

pm.test("response/students to be an array", () => { 
    console.log(typeof(jsonData.students));
    pm.expect(jsonData.students).to.be.an("array");     
});

One thing to note though is that your tests for the name, id and courses will fail as they are not targeting the array elements. You’ve got three records. Do you want to check those fields for all records? You can create a loop if you need to check each record.

I still recommend breaking your assertions into separate tests. In Postman, the first assertion in a test to fail will stop the rest of the code from executing.

Try and keep the assertions within a test to a minimum (Ideally singular).

Because the object test is failing and its the first assertion, you are not getting the feedback on the following assertions until you fix the first assertion.

I didn’t recommend removing the first part of your JSON, I said it was the only way I could replicate your issue.

My suggestion was to check that your JSON is as you believe it to be.
when I copy-paste your example, it works.
This would suggest that you are submitting something different to what you have shared in your example above.

1 Like

Hi sir, please find the attached screenshorts for your reference:




please let me know if you need any additional information.

Can we see the body tab in the Postman GUI please.

Not a notepad doc please.

Set it to pretty\JSON.

image

this is an endpoint issue …student/1

Hey @avionics-candidate21 :wave:

Are you offering/providing a solution or are you also having issues?

It’s difficult to establish from this message.