# Need solution for handling JSON error in Test Results

**URL:** <https://community.postman.com/t/need-solution-for-handling-json-error-in-test-results/61915>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [June 6, 2024, 12:56pm UTC](https://community.postman.com/t/need-solution-for-handling-json-error-in-test-results/61915 "2024-06-06T12:56:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramofficial1](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@ramofficial1](https://community.postman.com/u/ramofficial1)\
**Post date:** [June 6, 2024, 12:56pm UTC](https://community.postman.com/t/need-solution-for-handling-json-error-in-test-results/61915/1 "2024-06-06T12:56:04Z")

</div>

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.

 ![Issue with handling JSON error](https://us1.discourse-cdn.com/getpostman/original/3X/4/f/4fc812860e71a9a0578d24442a679d177d45c346.jpeg)

---

<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:** [June 6, 2024, 2:17pm UTC](https://community.postman.com/t/need-solution-for-handling-json-error-in-test-results/61915/2 "2024-06-06T14:17:00Z")

</div>

Hey @ramofficial1 👋

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.

---

<div class="post-metadata">

**Author:** ![ramofficial1](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@ramofficial1](https://community.postman.com/u/ramofficial1)\
**Post date:** [June 6, 2024, 2:23pm UTC](https://community.postman.com/t/need-solution-for-handling-json-error-in-test-results/61915/3 "2024-06-06T14:23:05Z")

</div>

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.

---

<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:** [June 6, 2024, 2:27pm UTC](https://community.postman.com/t/need-solution-for-handling-json-error-in-test-results/61915/4 "2024-06-06T14:27:10Z")

</div>

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. 😃

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 😃

---

<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 6, 2024, 2:33pm UTC](https://community.postman.com/t/need-solution-for-handling-json-error-in-test-results/61915/5 "2024-06-06T14:33:38Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![ramofficial1](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@ramofficial1](https://community.postman.com/u/ramofficial1)\
**Post date:** [June 6, 2024, 3:26pm UTC](https://community.postman.com/t/need-solution-for-handling-json-error-in-test-results/61915/6 "2024-06-06T15:26:00Z")

</div>

@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?

```auto
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");
    });
}

```

---

<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 6, 2024, 3:55pm UTC](https://community.postman.com/t/need-solution-for-handling-json-error-in-test-results/61915/7 "2024-06-06T15:55:03Z")

</div>

I would just have the following.

```auto
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…

```auto
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
});

```

---

<div class="post-metadata">

**Author:** ![ramofficial1](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@ramofficial1](https://community.postman.com/u/ramofficial1)\
**Post date:** [June 6, 2024, 6:40pm UTC](https://community.postman.com/t/need-solution-for-handling-json-error-in-test-results/61915/8 "2024-06-06T18:40:36Z")

</div>

Thanks @michaelderekjones for a simple solution.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [June 9, 2024, 6:41pm UTC](https://community.postman.com/t/need-solution-for-handling-json-error-in-test-results/61915/9 "2024-06-09T18:41:31Z")

</div>

Closed on June 9, 2024, 6:41pm UTC

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