Need solution for handling JSON error in Test Results

Whenever I add a code to create a response object i.e. let responseData = pm.response.json(); to get the response data and if there is an error code like 401, 403, 404, 405 there is an error displayed in Test Results tab as -
“There was an error in evaluating the test script: JSONError: Unexpected token ‘p’ at 1:5 404 page not found ^”

If i remove the line - let responseData = pm.response.json(); I don’t see the error in Test Results

Can someone please let me know how to handle this?

Please find the attached screenshot for details.

Hey @ramofficial1 :wave:

The error is shown because you’re trying to parse the response as JSON with that line. I can’t see the body of that response but i’m guessing it’s plain text as the pm.response.text() function is working there.

You could wrap that in a conditional statement to only fire when a certain response code is returned.

I’m not sure what you’re trying to do on that particular test though, seems like it has no real focus if you’re using to test for every single possible scenario.

1 Like

Thanks Danny. I just captured only a sample code to show the error and hence there is no complete code in the screenshot. As suggested will implement this using conditional statement.

I doesn’t really matter if it’s 1 line or a 1000 lines, if the response isn’t JSON it’s going to error. :smiley:

Adding a conditional statement is a way to get around it but I would look more into the test design as a whole and establish what it is you’re actually testing. Does every scenario need to be accounted for in that particular request?

I’m sure that @michaelderekjones has many thoughts here :smiley:

Personally, I dislike conditional statements in testing.

If you are doing data driven testing, so it’s same request with different data, then that might be a use case for a conditional statement related to the status code.

You should be in control of your test data and a request should return the same response every time.

In the situation above, I would wrap all of the code in the tests tab in an initial test for the status code.

You put the assertion for the status code, and then follow that line with the code to parse the response.

If the status code assertion fails, it will stop executing any more code immediately so the command to parse the response will never run (preventing the runtime error).

1 Like

@michaelderekjones , I tried the below sample code and it seems to work. It works for status code 200 and 404 as of now. I will include other status code as well. Does it look good?

if([200,201].includes(pm.response.code)){
var jsonData = pm.response.json();

    pm.test("Check status code is 200", function () {
        pm.response.to.have.status(200);
    });
    
    pm.test("The body is JSON", function () {
        pm.response.to.be.withBody;
        pm.response.to.be.json;
    });

}
else if (pm.expect(pm.response.code).to.eql(404)){
    pm.test("HTML error - "+pm.response.code, () => {
        pm.expect.fail("Failed");
    });
}

I would just have the following.

pm.test("Check status code is 200", function () {
    pm.response.to.have.status(200);
    var jsonData = pm.response.json();
    // further tests go here
});

If the response is 404, then it will fail the test and won’t run the line to parse the response.

If you need to cater for 201 as well, you could have…

pm.test("Check status code is 200/201", function () {
    pm.expect(pm.response.code).to.be.oneOf([200,201]);
    var jsonData = pm.response.json();
    // further tests go here
});
1 Like

Thanks @michaelderekjones for a simple solution.

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