Strip comments from JSON strings while preserving original positioning for accurate error reporting
npx @tessl/cli install tessl/npm-strip-json-comments@5.0.0Strip JSON Comments is a JavaScript utility library that removes single-line (//) and multi-line (/* */) comments from JSON strings while preserving original character positions for accurate error reporting. It enables the use of comments in JSON files, which is particularly useful for configuration files and development environments.
npm install strip-json-commentsimport stripJsonComments from "strip-json-comments";Note: This package is ESM-only and cannot be imported with require(). Use import syntax or dynamic imports.
import stripJsonComments from "strip-json-comments";
const json = `{
// Rainbows
"unicorn": /* ❤ */ "cake"
}`;
const cleanJson = stripJsonComments(json);
JSON.parse(cleanJson);
//=> {unicorn: 'cake'}Removes comments from JSON strings while maintaining original character positions for accurate error reporting.
/**
* Strip comments from JSON. Lets you use comments in your JSON files!
* @param jsonString - JSON string that may contain comments
* @param options - Configuration options
* @returns JSON string without comments
*/
function stripJsonComments(jsonString: string, options?: Options): string;Parameters:
jsonString (string, required): JSON string that may contain single-line (//) or multi-line (/* */) commentsoptions (object, optional): Configuration options for comment stripping behaviorReturns: string - JSON string with comments removed according to the specified options
Throws: TypeError if jsonString parameter is not a string
Usage Examples:
// Basic comment stripping (default whitespace replacement)
const jsonWithComments = `{
// This is a comment
"name": "example",
/* Multi-line
comment here */
"value": 42
}`;
const cleaned = stripJsonComments(jsonWithComments);
// Comments are replaced with equivalent whitespace
// Remove comments entirely (no whitespace replacement)
const minimal = stripJsonComments(jsonWithComments, { whitespace: false });
// Strip trailing commas in addition to comments
const jsonWithTrailing = `{
"name": "example",
"value": 42,
}`;
const noTrailing = stripJsonComments(jsonWithTrailing, { trailingCommas: true });interface Options {
/**
* Strip trailing commas in addition to comments.
* @default false
*/
readonly trailingCommas?: boolean;
/**
* Replace comments and trailing commas with whitespace instead of stripping them entirely.
* @default true
*/
readonly whitespace?: boolean;
}Configuration object for customizing comment stripping behavior.
Properties:
trailingCommas (boolean, optional): When true, removes trailing commas from objects and arrays in addition to comments. Default: falsewhitespace (boolean, optional): When true, replaces comments with equivalent whitespace to preserve character positions. When false, removes comments entirely. Default: trueBy default, comments are replaced with whitespace rather than removed entirely. This ensures that:
// comment text/* comment text */// within /* */ blocksComments inside JSON strings are preserved and not stripped:
const jsonString = '{"url": "https://example.com//path"}';
stripJsonComments(jsonString); // Comments in strings are NOT removedOptional removal of trailing commas from JSON objects and arrays:
const json = `{
"name": "example",
"items": [1, 2, 3,],
}`;
stripJsonComments(json, { trailingCommas: true });
// Removes trailing commas while handling commentsTypeError for non-string inputsProperly handles different line ending formats:
\n)\r\n)