There is alternative package on NPM: json-easy-strip
The main idea is to use just one-liner RegExp to strip all type of JS-style comments. And yes, it's simple and very possible! Package is more advanced, with some file caching etc, but still simple. Here is the core:
sweet.json
{
/*
* Sweet section
*/
"fruit": "Watermelon", // Yes, watermelons is sweet!
"dessert": /* Yummy! */ "Cheesecake",
// And finally
"drink": "Milkshake - /* strawberry */ or // chocolate!" // Mmm...
}
index.js
const fs = require('fs');
const data = (fs.readFileSync('./sweet.json')).toString();
// Striper core intelligent RegExp.
// The idea is to match data in quotes and
// group JS-type comments, which is not in
// quotes. Then return nothing if there is
// a group, else return matched data.
const json = JSON.parse(data.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m));
console.log(json);
// {
// fruit: 'Watermelon',
// dessert: 'Cheesecake',
// drink: 'Milkshake - /* strawberry */ or // chocolate!'
// }
So now because you was asking about ShellScripting-style comments
#
# comments
#
We can extend our RegExp by adding \#.*
at the end of it:
const json = JSON.parse(data.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/|\#.*)/g, (m, g) => g ? "" : m));
Or, if you don't want JS-style comments at all:
const json = JSON.parse(data.replace(/\\"|"(?:\\"|[^"])*"|(\#.*)/g, (m, g) => g ? "" : m));