What is comment-json?
The comment-json npm package allows you to parse, modify, and stringify JSON files while preserving comments. This is particularly useful for configuration files where comments are often used to provide context or instructions.
What are comment-json's main functionalities?
Parsing JSON with comments
This feature allows you to parse JSON strings that include comments. The comments are ignored in the resulting JavaScript object.
const commentJson = require('comment-json');
const jsonWithComments = `{
// This is a comment
"key": "value"
}`;
const parsed = commentJson.parse(jsonWithComments);
console.log(parsed); // { key: 'value' }
Stringifying JSON with comments
This feature allows you to convert a JavaScript object back into a JSON string, optionally including comments.
const commentJson = require('comment-json');
const obj = { key: 'value' };
const jsonString = commentJson.stringify(obj, null, 2);
console.log(jsonString); // {
// This is a comment
"key": "value"
}
Modifying JSON while preserving comments
This feature allows you to modify a parsed JSON object and then convert it back to a JSON string, preserving the original comments.
const commentJson = require('comment-json');
const jsonWithComments = `{
// This is a comment
"key": "value"
}`;
let parsed = commentJson.parse(jsonWithComments);
parsed.newKey = 'newValue';
const modifiedJsonString = commentJson.stringify(parsed, null, 2);
console.log(modifiedJsonString); // {
// This is a comment
"key": "value",
"newKey": "newValue"
}
Other packages similar to comment-json
json5
JSON5 is a JSON extension that aims to make JSON more human-friendly. It allows comments, trailing commas, and more. However, unlike comment-json, JSON5 does not preserve comments when parsing and stringifying.
hjson
Hjson is a user interface for JSON. It allows comments and is designed to be easy to read and write. Similar to JSON5, Hjson does not preserve comments when converting between JSON and JavaScript objects.
jsonc-parser
The jsonc-parser package is used by Visual Studio Code to handle JSON with comments. It can parse JSON with comments and provide a syntax tree, but it is more complex to use compared to comment-json.
Parse and stringify JSON file with domments.
Install
$ npm install comment-json --save
Usage
package.json:
{
"name": "comment-json"
}
var json = require('comment-json');
var obj = json.parse(fs.readFileSync('package.json').toString());
console.log(obj);
json.stringify(obj, null, 2);
json.parse(string, [reviver])
The arguments are the same as the vanilla JSON.parse
.
json.stringify(object, [replacer], [space])
The arguments are the same as the vanilla JSON.stringify
.
And it does the similar thing as the vanilla one, and also stringify the "// abc"
-like property into comments if the "abc"
property is found.
json.strip(string)
Strips comments from string
.
json.clean(object)
Clean comment properties.
var object = {
"// name": "// package name",
name: "comment-json"
};
json.clean(object);
License
MIT