Error Paring JSON response -- JSONError: Unexpected token u

Iā€™m developing a pre-request script. This script does a POST to get a JWT that Iā€™ll use for my main request. The POST returns with a http 200 so all goodā€¦ if I comment out the attempted parse, it all completes with no issue. I have a result that passes JSONlint ā€¦ so I didnā€™t expect any issues.

Its a simple response and I simply want to get the ā€œvalā€ attribute but the JSONParse fails.
The Postman console says ā€œJSONError: Unexpected token u in JSON at position 0ā€ and as you can see thereā€™s no ā€œuā€ attribute in the response. Yes, I see that inside the value for ā€œvalā€ the letter ā€˜uā€™ turns upā€¦ that canā€™t be the issue.

How can I sort out whatā€™s happening so I can make this work?

{
ā€œttā€: ā€œJWTā€,
ā€œvalā€: ā€œeyJraWQiOiJJYW1Kd2tLZXkiLCJhbGciOiJFUzI1NiJ9.eyJqdGkiOiJKMFZjazlsaDc2QlVQdnBrenJVU3hnIiwiaWF0IjoxNjEyMzk0NjUwLCJpc3MiOiJJQU0iLCJhdWQiOiIxMjduamNsZW14OSIsInN1YiI6IjEyN25qY2xlbXg5IiwiZXhwIjoxNjEyMzk1NTUwLCJ0a25mbyI6IntcImFjY2Vzc190b2tlblwiOlwiYTFhY2E2YjctMmZhZS00MDExLTg0OTEtM2M3ODJmMTZhYmI0XCIsXCJzY29wZVwiOltcInNlcnZpY2VJbnN0YW5jZTovY29tLmZpY28uZG1wLnMzL3Jlc3Qvc2VydmljZS9zb2x1dGlvbmluc3RhbmNlcy8xMjdubzB4c3kxaC9hcGlcIixcIlBVVDpjb25zb2xlLmRtcy5wcmV2aWV3LnVzdzIuZmljb2FuYWx5dGljY2xvdWQuY29tXCIsXCJzb2x1dGlvbklkOjEyN25qY2xlbXg5XCIsXCJvPUZJQ08tRklFTEQtVEVOQU5UXCIsXCJzZXJ2aWNlSW5zdGFuY2U6L2NvbS5maWNvLmRtcC5zMy9yZXN0L3NlcnZpY2Uvc29sdXRpb25pbnN0YW5jZXMvMTI3bm84dTk1NGIvYXBpXCIsXCJjbGllbnRfaWQ6MTI3bmpjbGVteDlcIixcIl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klDTy1GSUVMRC1URU5BTlRcIixcImVuYWJsZWxjbVwiOlwidHJ1ZVwiLFwibWF4Y29tcG9uZW50c1wiOlwiNVwiLFwiZW5hYmxlZmFzdFwiOlwidHJ1ZVwifSxcInJlYWxtXCI6XCIvRmljb0FuYWx5dGljQ2xvdWRcIixcInRva2VuX3R5cGVcIjpcIkJlYXJlclwiLFwiZXhwaXJlc19pblwiOjM1OTksXCJvXCI6XCJGSUNPLUZJRUxELVRFTkFOVFwifSIsImtpZCI6IkZpY29BbmFseXRpY0Nsb3VkIn0.EuD6sBbUfFYSuJ7EgZdz7s5j4bksLl9nlfu1-i0mZSnREnXf_GWTCiu0FA34AZnSKzTjW_Lu82lRnx6taoQ6Vwā€,
ā€œvā€: ā€œ1.0ā€,
ā€œexpā€: 1612395550
}

I think I see whatā€™s happening hereā€¦ but I donā€™t know what to do about it. If I manually create a request thatā€™s a POST as above (same content as in my pre-request script) I get a response bodyā€¦ I can see it in the UI (same as above) ā€¦ but if I make a POST in the pre-request script I donā€™t have a bodyā€¦ I have status and status-code and the response headers, streamā€¦ but thereā€™s no body like I would have if I made a GET.

I canā€™t use GET, I have to use POST (its not my interface) ā€¦

Is there anything I can do?

Can you try to do log of response before parsing to JSON.
console.log(response.body);
And check how data is coming in response.
If there is no data for it try to do console.log(response);
Then we can try to extract the data

May be this blog can help you :slight_smile:

1 Like

You donā€™t need to parse that response like that, itā€™s not the same as the main response.

You would just need to use this in the sendRequest() function:

pm.environment.set('jwt-token', response.json().val)
1 Like

Thanks, I tried that even before posting - console.info( response.body );
I get ā€œundefinedā€ in the logsā€¦ which is why Iā€™m seeing the ā€œuā€ parse error that complains about an unexpected token ā€œuā€ ā€¦

I DO have a response object.

{id: ā€œ344e2763-ca69-46e4-a10c-f8527b5bc2d9ā€, status: ā€œOKā€, code: 200ā€¦}

  1. id: ā€œ344e2763-ca69-46e4-a10c-f8527b5bc2d9ā€
  2. status: ā€œOKā€
  3. code: 200
  4. :arrow_forward:header: [5]
  5. :arrow_forward:stream: {ā€¦}
  6. cookie: [0]
  7. responseTime: 3960
  8. responseSize: 2229

BINGO, I think Iā€™m getting some where ā€¦

Thanks, Iā€™ll review and maybe thereā€™s another trick in there for me to learn.