or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-strip-json-comments

Strip comments from JSON strings while preserving original positioning for accurate error reporting

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/strip-json-comments@5.0.x

To install, run

npx @tessl/cli install tessl/npm-strip-json-comments@5.0.0

index.mddocs/

Strip JSON Comments

Strip 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.

Package Information

  • Package Name: strip-json-comments
  • Package Type: npm
  • Language: JavaScript/TypeScript (ESM only, requires Node.js 14.16+)
  • Installation: npm install strip-json-comments

Core Imports

import stripJsonComments from "strip-json-comments";

Note: This package is ESM-only and cannot be imported with require(). Use import syntax or dynamic imports.

Basic Usage

import stripJsonComments from "strip-json-comments";

const json = `{
	// Rainbows
	"unicorn": /* ❤ */ "cake"
}`;

const cleanJson = stripJsonComments(json);
JSON.parse(cleanJson);
//=> {unicorn: 'cake'}

Capabilities

Strip JSON Comments

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 (/* */) comments
  • options (object, optional): Configuration options for comment stripping behavior

Returns: 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 });

Types

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;
}

Options Interface

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: false
  • whitespace (boolean, optional): When true, replaces comments with equivalent whitespace to preserve character positions. When false, removes comments entirely. Default: true

Key Features

Position Preservation

By default, comments are replaced with whitespace rather than removed entirely. This ensures that:

  • JSON parsing error line/column numbers remain accurate
  • Original source mapping is preserved
  • Debugging maintains correct position references

Comment Types Supported

  • Single-line comments: // comment text
  • Multi-line comments: /* comment text */
  • Nested comment patterns: Handles complex cases like // within /* */ blocks

String Handling

Comments inside JSON strings are preserved and not stripped:

const jsonString = '{"url": "https://example.com//path"}';
stripJsonComments(jsonString); // Comments in strings are NOT removed

Trailing Comma Support

Optional 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 comments

Error Handling

  • Validates input type and throws TypeError for non-string inputs
  • Handles malformed or incomplete comment blocks gracefully
  • Manages edge cases like escaped quotes and complex string patterns

Line Ending Support

Properly handles different line ending formats:

  • Unix (\n)
  • Windows (\r\n)
  • Mixed line endings within the same document