Pre-request scripts (JSON.parse) & comments in the body

Good afternoon,

I was wondering if anyone could help with this. I know, I think, why its not working but not sure of the solution and keep hitting a dead end.

I have these lines of code in the pre-request script:
image

The token and account variables are then validated to see what is populated or not.

If i add a comment into the request body i get this error
image

Which makes sense, as the comment, or commented out fields of the request aren’t JSON.

Is there a way around this? How can I add comments or comment out fields and still have a pre-request script validating fields?

Thanks,
Chris

Hi @chrisdoar. Welcome to the Postman Community :postman:.

What version of Postman are you currently on? Ideally, pm.request.body.raw should strip away JSON comments from the request body and post only the raw JSON.

Hello @gbadebo-bello

I am version 11.2.35 of Postman.

Its not a JSON comment in the request body, just a comment or commented out data item, like this:

My current setup for testing values fails as lines 1 and 13 are valid JSON, presumably. That’s my theory anyway. I’d have thought there would be a way have comments in the request, but not have then parsed.

Thanks,
Chris

JSON is data-only. Key/Value pairs.

If you include a comment, then it must be data too.

In short, lines 1 and 13 are not valid JSON key/value pairs.

Can comments be used in JSON? - Stack Overflow

The JSON.parse function will be expecting the first character to be an { or [ to denote an object or array.

If in doubt, just console log “pm.request.body.raw”. If its still got the comments in there, then the parse will fail.

However I have just tested this with the Postman Web UI, and pm.request.body.raw does strip comments if they exist from the body so checking your version number and console logging it directly would seem appropriate troubleshooting steps.

2 Likes

Comments are inherently not supported by JSON. To aid better collaboration, we introduced JSON comments in Postman v11 release.

JSON comments can cause errors if included in the JSON gotten programmatically from the script tab, so the scripting library does the heavy lifting of stripping the comments off when accessed programmatically.

@chrisdoar a screenshot of what you see in the logs will be really helpful here as Mike recommended.

Thank you very much @gbadebo-bello and @michaelderekjones for your replies. Apologies for the delayed response. Actual work has got in the way of random playing around with API’s!

I’ve added a line to log the request body: console.log(pm.request.body.raw). This has, as expected, returned the comments within the request body

I get the parse fails as the comment following // isn’t valid JSON. My desktop version of Postman is 11.2.0. I’ll remove the comments then it will work for now but continue to look into a solution that will allow me to add comments.

Thanks,
Chris

Hey @chrisdoar. One thing I suspect here could be the formatting of the JSON itself. The comma seperators does look a bit off. But ideally, that should not be the case. I know we’ve had issues with JSON formatted as ascii in the past(not entirely sure)

I’ve done a bit more testing on this, and there are some discrepancies on how this is working.

Based on this body with the comments added in various places.

// comment 1
{    
   "key1" : "value1" // comment 2
   ,"key2" : "value2"
 //  "key3" : "value3"
}

I’ve put the following code in the pre-request script and the post response. (My earlier test had the code only in the post response script).

let body = pm.request.body.raw;
console.log(body);

With the following results.

@gbadebo-bello The pre-request script is not stripping the comment, but the post-response script with the same code is. Therefore the JSON.parse will work in the post-response script, but not in the pre-request script.

Looking at the body that was sent, you can see that the comments are stripped at that point.

1 Like

Thank you for that @michaelderekjones. Looking at what you’ve done I’ve replicated it on my API call and can see the comments aren’t stripped from the body on the pre-request script but are on the post-request script

Thanks,
Chris

Thanks for catching this @michaelderekjones . That’s super interesting and I’ll forward this to the relevant team as it should be working on both pre-request and post-response scripts.

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