Reputation: 1882
How can i read this file 'file.json':
# Comment01
# Comment02
{
"name": "MyName"
}
and retrieve the json without comments?
I'm using this code:
var fs = require('fs');
var obj;
fs.readFile('./file.json', 'utf8', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
});
it returns this error:
SyntaxError: Unexpected token # in JSON at position 0
Have npm some package to solve this question?
Upvotes: 9
Views: 8721
Reputation: 19949
let json = require('json5')
let fs = require('fs-extra')
let a = json.parse(fs.readFileSync("./tsconfig.json"))
console.log(a)
you can use json5 module
Upvotes: 2
Reputation: 9
Javascript has a comment remover built-in, no need for an extra package. I wouldn't do this for user input though.
eval(
'var myjsonfile=' +
require('fs')
.readFileSync('./myjsonfile.json')
.toString(),
);
console.log(myjsonfile);
Upvotes: -3
Reputation: 1882
The perfect package for this problem is https://www.npmjs.com/package/hjson
hjsonText input:
{
# hash style comments
# (because it's just one character)
// line style comments
// (because it's like C/JavaScript/...)
/* block style comments because
it allows you to comment out a block */
# Everything you do in comments,
# stays in comments ;-}
}
Usage:
var Hjson = require('hjson');
var obj = Hjson.parse(hjsonText);
var text2 = Hjson.stringify(obj);
Upvotes: 10
Reputation: 2549
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));
Upvotes: 4
Reputation: 19428
You can use your own RegExp
pretty easily to match the comments beginning with a #
const matchHashComment = new RegExp(/(#.*)/, 'gi');
const fs = require('fs');
fs.readFile('./file.json', (err, data) => {
// replaces all hash comments & trim the resulting string
let json = data.toString('utf8').replace(matchHashComment, '').trim();
json = JSON.parse(json);
console.log(json);
});
Upvotes: 4
Reputation: 8121
The package you are looking for is called strip-json-comments - https://github.com/sindresorhus/strip-json-comments
const json = '{/*rainbows*/"unicorn":"cake"}';
JSON.parse(stripJsonComments(json)); //=> {unicorn: 'cake'}
Upvotes: 2