strip-json-comments
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The npm package "strip-json-comments" is designed to remove comments from JSON files. This includes both single-line comments (marked with //
) and multi-line comments (marked with /**/
). By replacing these comments with whitespace, it preserves JSON error positions, aligning them as closely as possible to the original code. Furthermore, this package allows you to incorporate comments in your JSON files for improved readability without affecting their functionality.
The "strip-json-comments" package is straightforward to use:
First, install it using your Node.js package manager, like so:
npm install strip-json-comments
Then, you can use it in your JavaScript code to strip comments from a JSON string.
Here's an example:
import stripJsonComments from 'strip-json-comments';
const json = `{
// A rainbow comment
"unicorn": /* Heart emoji comment */ "cake"
}`;
JSON.parse(stripJsonComments(json));
// This yields: {unicorn: 'cake'}
In this example, the JSON string includes two comments. With the stripJsonComments()
function, these are removed, and you can parse the result into a valid JSON object. The function also comes with options to handle trailing commas and replace comments with whitespace.
The documentation for the "strip-json-comments" package can be found in its README file on the GitHub repository, available at https://github.com/sindresorhus/strip-json-comments. This includes detailed usage instructions, API descriptions, and links to related packages.