Officially comments are not supported in the JSON format. In fact this lack of ability to comment is one of the reasons that lead to the downfall of the JSON based project system during the rewrite of the .NET some years back. However they sure can be useful to support. In my case I wanted to add some comments to the body of a request to explain a parameter in Postman. I like to keep comments as close to the thing they describe as possible so I didn't want this on a wiki somewhere nobody would ever find.
The content looked something like
This was going to an ExpressJS application which was parsing the body using
body-parser. These days we can just use
express.json() and avoid taking on that additional dependency. The JSON parsing in both these is too strict to allow for comments. Fortunately, we can use middleware to resolve the issue. There is a swell package called
strip-json-comments which does the surprisingly difficult task of stripping comments. We can use that.
The typical json paring middleware looks like
Instead we can do
import stripJsonComments from 'strip-json-comments';
This still allows us to take advantage of the compression and character encoding facilities in the original parser while also intercepting and cleaning up the JSON payload.