{"info":{"_postman_id":"669c4b93-b9be-4950-8bc0-9c53b457c1a9","name":"Use comments in request body","description":"<html><head></head><body><img src=\"https://cdn2.hubspot.net/hubfs/208250/Blog_Images/postman.jpg\">\n\n<h1 id=\"description\">Description</h1>\n<p>This collection shows how to add comments to JSON request body. Here we use JSON5 library using cdn and use to remove comments before sending the request</p>\n<h1 id=\"usage-instruction\">Usage instruction</h1>\n<p><strong>Adding comments in request body:</strong></p>\n<p><strong>Step 1.</strong></p>\n<p>RUN oneTimeRequest-<strong>JSON5</strong> request . this will get the JSON5 library from <strong>cdnjs</strong> ( a JS library hosting service) and store the content as collection variable <strong>json5</strong></p>\n<p><strong>This is a one time operation</strong></p>\n<p><strong>Step 2.</strong></p>\n<p>now you can send the <strong>createUserExample-With comments</strong> and <strong>createUserExample-Wtout comments</strong> requests to see how it works</p>\n</body></html>","schema":"https://schema.getpostman.com/json/collection/v2.0.0/collection.json","toc":[{"content":"Description","slug":"description"},{"content":"Usage instruction","slug":"usage-instruction"}],"owner":"8296678","collectionId":"669c4b93-b9be-4950-8bc0-9c53b457c1a9","publishedId":"2s8ZDVb4fY","public":true,"customColor":{"top-bar":"FFFFFF","right-sidebar":"303030","highlight":"EF5B25"},"publishDate":"2023-01-18T18:10:24.000Z"},"item":[{"name":"oneTimeRequest-JSON5","event":[{"listen":"prerequest","script":{"id":"0ff7d320-e169-4d9e-95c3-9bcddd36d142","exec":["//run this request to store JSON5 cdnjs as enviroment variable","/**"," * "," * you can use send request in pre-request also to do this "," * ","pm.sendRequest(\"https://cdnjs.cloudflare.com/ajax/libs/json5/2.2.3/index.js\", (mtzErr, mtzRes) => {","    pm.collectionVariables.set(\"json5\", mtzRes.text());","})","*/",""],"type":"text/javascript"}},{"listen":"test","script":{"id":"6c06afac-fd70-47a6-aab2-a7d893f37950","exec":["pm.collectionVariables.set(\"json5\", pm.response.text());"],"type":"text/javascript"}}],"id":"c2a8b8c1-acf9-4f01-9433-1d9d6223d221","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[],"body":{"mode":"raw","raw":"","options":{"raw":{"language":"json"}}},"url":"https://cdnjs.cloudflare.com/ajax/libs/json5/2.2.3/index.js","description":"<p>This request stores the JSON5 library contend from cdnjs to collection variable 'json5' which can be used to parse comments in json</p>\n","urlObject":{"protocol":"https","path":["ajax","libs","json5","2.2.3","index.js"],"host":["cdnjs","cloudflare","com"],"query":[],"variable":[]}},"response":[],"_postman_id":"c2a8b8c1-acf9-4f01-9433-1d9d6223d221"},{"name":"createUserExample-With comments","event":[{"listen":"prerequest","script":{"id":"0ff7d320-e169-4d9e-95c3-9bcddd36d142","exec":["","/**"," * "," * if you want to use it across many request during collection run"," * use Function instead "," * (new Function(pm.collectionVariables.get('json5')))()"," * "," * As function is executed in global scope , you will get JSON5 as a global varible "," * you can use it across multiple request in collection runner as JSON5.parse() in tead of calling eval and then calling this.JSON5 for each request"," * */","","","// call eval in each request as this is executed per request , else this.JSON5 will be undefined","eval(pm.collectionVariables.get('json5'))","","pm.request.body= this.JSON5.parse(pm.request.body)"],"type":"text/javascript"}}],"id":"4311dfe7-c0a6-454a-9c7e-bc9978b4a851","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[],"body":{"mode":"raw","raw":"{\n\n    //Please add the comments in any valid Javascript format\n\n    /**\n    multiline comments\n    */\n    \"name\": \"morpheus\", // single line comments \n    \"job\": \"leader\"\n}","options":{"raw":{"language":"json"}}},"url":"https://reqres.in/api/users","description":"<p>This request uses json5 in pre-request script to parse json body before sending request , hence sends the request successfully</p>\n","urlObject":{"protocol":"https","path":["api","users"],"host":["reqres","in"],"query":[],"variable":[]}},"response":[],"_postman_id":"4311dfe7-c0a6-454a-9c7e-bc9978b4a851"},{"name":"createUserExample-Wtout comments","event":[{"listen":"prerequest","script":{"id":"0ff7d320-e169-4d9e-95c3-9bcddd36d142","exec":[""],"type":"text/javascript"}}],"id":"48bfdbc9-49f7-4cfc-8aea-ee3307f56cdd","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[],"body":{"mode":"raw","raw":"{\n\n    //Please add the comments in any valid Javascript format\n\n    /**\n    multiline comments\n    */\n    \"name\": \"morpheus\", // single line comments \n    \"job\": \"leader\"\n}","options":{"raw":{"language":"json"}}},"url":"https://reqres.in/api/users","description":"<p>This request uses json body with comments but doesnt parse it using json5 hence gets 400 error, as json with comments is not a valid json</p>\n","urlObject":{"protocol":"https","path":["api","users"],"host":["reqres","in"],"query":[],"variable":[]}},"response":[],"_postman_id":"48bfdbc9-49f7-4cfc-8aea-ee3307f56cdd"}],"event":[{"listen":"prerequest","script":{"id":"aa2c977a-309a-4db2-adfa-e9fa58e00519","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"699f5b6d-e4c2-4b49-9ec6-5d4f99c30844","type":"text/javascript","exec":[""]}}],"variable":[{"key":"json5","value":""}]}