strip-json-comments
Strip comments from JSON strings so you can safely use JSONC config files.
Repository Health
Technical Analysis
strip-json-comments removes // and /* */ style comments (and optionally trailing commas) from JSON strings before parsing, letting you author human-friendly JSONC configuration files while still producing standards-compliant JSON.parse() output. It preserves character offsets by replacing stripped comments with whitespace by default, so error positions reported by JSON.parse() still point at the original source location, which matters when debugging a malformed config file.
What You Get
- A single default export, stripJsonComments(), that takes a JSON string and returns a clean JSON string
- Support for both // single-line and /* */ multi-line comment styles inside JSON
- An optional trailingCommas flag to also strip trailing commas before parsing
- A whitespace option to choose between blanking comments (preserving character offsets) or fully removing them
Common Use Cases
- Reading a JSONC config file (e.g. tsconfig.json-style files) and parsing it as regular JSON
- Building a config-loading layer for a CLI tool or app that wants to let users comment their JSON settings
- Powering higher-level JSONC parsers and editors that need to tolerate comments in JSON
Under The Hood
Architecture: The entire library is a single exported function, stripJsonComments(), that walks the input string one character at a time with a small state machine (isInsideString / isInsideComment as singleComment or multiComment via Symbols) plus an offset/buffer pair used to flush non-comment slices as it goes; an optional second pass handles trailingCommas by tracking the last seen comma index and only stripping it once a closing } or ] confirms it was trailing. Tech Stack: Pure, dependency-free modern JavaScript (ESM only, type: module, Node >=14.16) with hand-written .d.ts type definitions, tested with ava and type-checked with tsd; xo enforces lint rules and matcha runs the benchmark script. Code Quality: test.js exercises single-line comments, multi-line comments, comments inside strings, escaped quotes, both whitespace modes, and the trailingCommas option; the code favors small pure functions (isEscaped, stripWithWhitespace, stripWithoutWhitespace) over classes, with no external error-handling surface beyond a single TypeError guard on the input type. API Design: The API is minimal and ergonomic, one default export plus two boolean options (whitespace, trailingCommas) with sensible defaults, requiring zero configuration for the common case of import stripJsonComments from ‘strip-json-comments’.
Used by 5 apps in this directory
AionUi
AI Agents · Productivity
Free, open-source Cowork desktop app that unifies Claude Code, Codex, Gemini CLI, and 20+ AI agents into a single platform with multi-agent teams, 24/7 cron automation, and zero-config built-in agent.
CodeSandbox
Code Editors · Developer Tools
Instantly ready browser-based IDE that runs full npm dependency resolution and transpilation entirely client-side, with no server needed.
Mastra Code
AI Code Assistants
"A coding agent that never compacts" — a terminal-based AI coding agent built on the Mastra framework, with Observational Memory instead of context compaction, multi-model support, and OAuth login for Claude Max or ChatGPT Plus.
Sourcebot
Search · Developer Tools · AI Code Assistants
A self-hosted, AI-powered code search engine that indexes every repo across GitHub, GitLab, Bitbucket, Gitea, Gerrit, and Azure DevOps, so both engineers and coding agents can search, browse, and ask questions about your codebase from one place.
Webiny JS
Ecommerce · Blogging · CMS
Open-source, self-hosted CMS on AWS serverless — a TypeScript framework you extend with code, not a product you configure through a UI.