I’m reading json files in Node.js using require("fs").
Something like:
var readJsonFromFile= function(fileLocation, callback){
fs.readFile(fileLocation, 'utf8', function (err, data) {
if (err) {
return callback(err);
}
data = JSON.parse(data);
callback(null,data);
});
}
However, I noticed JSON.parse:
- doesn’t allow comments
// blaor/* blaa */ - requires keys to be quoted.
Although I realize this is technically correct, I’d like to know if any small library exists which cleans my often annotated json-files to guarentee the above. (And no, it’s not completely trivial DIY, think // as part of valid values, etc. )
Thanks
Yes! I use JSON.minify by Kyle Simpson for this very purpose:
https://github.com/getify/JSON.minify
It isn’t a full-blown Node module, but it works very well for loading JSON-like config files and such. Note that you still have to quote your keys, but it does allow for comments.