Choosing an npm Package for JSON With Comments
Five packages, the same nine inputs, one real pass or fail for each.
If a config file has comments in it, JSON.parse will not read it. Five npm packages fix that in different ways: json5, jsonc-parser, comment-json, strip-json-comments and hjson. They accept different syntax, only some of them can write a file back, and fewer still keep the comments when they do.
We installed all five, ran the same inputs through each, tested an edit and write-back, and read the version, size, license and download figures from the npm registry on 12-01-2026. A decision table at the end maps each job to a package.
TL;DR: To read a JSON5 file, use json5; it passed all nine inputs. To read and edit JSONC and keep the comments, use comment-json (whole objects) or jsonc-parser (small edits). To just delete comments before JSON.parse, use strip-json-comments. hjson reads a different, friendlier format. None of the five edits a JSON5 file with single quotes and keeps its comments; see preserving comments. Tested 12-01-2026.
What Each Package Parses
One test file ran the same nine strings through each package on Node.js 24.22.1. "Yes" means the call returned the expected value with no error. For jsonc-parser we also counted a non-empty error list as a failure, because its README says to always check that list. strip-json-comments only removes text, so we passed its output to JSON.parse. JSON.parse is the control column.
| Input | json5 2.2.3 | jsonc-parser 3.3.1 | comment-json 5.0.0 | strip-json-comments 5.1.0 | hjson 3.2.2 | JSON.parse |
|---|---|---|---|---|---|---|
| Strict JSON | Yes | Yes | Yes | Yes | Yes | Yes |
Line comment // | Yes | Yes | Yes | Yes | Yes | No |
Block comment /* */ | Yes | Yes | Yes | Yes | Yes | No |
| Trailing comma | Yes | No (Yes with allowTrailingComma) | Yes | No (Yes with trailingCommas) | Yes | No |
| Single-quoted string | Yes | No | No | No | Yes | No |
| Unquoted key | Yes | No | No | No | Yes | No |
Hex number 0xFF | Yes | No | No | No | No (reads a string or throws) | No |
Infinity | Yes | No | No | No | No (reads a string or throws) | No |
| Backslash line continuation in a string | Yes | No | No | No | No | No |
Reading the table:
- Only
json5passed all nine. That fits the specification, which allows every one of these forms. Because it also reads comments and trailing commas, it reads JSONC files too. - Trailing commas are an option in two packages.
jsonc-parserfailed them by default and passed withallowTrailingComma: true.strip-json-commentsfailed them by default and passed withtrailingCommas: true. Every other row failed even with those options on. jsonc-parserreturns a value even when it fails. For{a:1}it reported errors and returned{}; for a hex number it returned0. If you skip the errors list you get quietly wrong data.hjsonparses hex and Infinity into strings. On a multi-line object,a: 0xFFgave{"a":"0xFF"}anda: Infinitygave{"a":"Infinity"}, because Hjson reads an unquoted value as a string. On a one-line object the same input threw. Hjson has its own multi-line form, a block between'''lines, which parsed to"x\ny".- The JSONC specification gap is real. For the full JSONC story, including how the draft spec treats trailing commas, see JSON5 vs JSONC.
Can It Write, and Are the Comments Kept?
Reading is half the job. The test file below has a line comment and a block comment. We changed port from 8080 to 9090 with each package and wrote the text back.
{
// port to listen on
"port": 8080, /* keep above 1024 */
"name": "demo"
}| Package | How it writes | Comments after the edit |
|---|---|---|
json5 | JSON5.stringify(object) | Lost. Output was { port: 9090, name: 'demo', } across three lines, in JSON5 style. |
jsonc-parser | modify returns edits, applyEdits applies them. No object writer. | Kept. Only 8080 changed; both comments stayed where they were. |
comment-json | stringify(object, null, 2) | Kept. Both comments were written back in place. |
strip-json-comments | Cannot write. It returns text with the comments replaced by spaces. | Removed on purpose. |
hjson | Hjson.stringify(object), or Hjson.rt.stringify for round trips | stringify lost them. Hjson.rt kept both, and the output used Hjson style (no quotes on keys or values, no commas). |
The two that preserved comments worked differently. comment-json holds comments in symbol properties of the parsed object, so you edit a plain-looking object and call stringify. Its README notes that without the space argument the comments are not written, and that trailing commas are removed on output; our run of {"a":1,} printed {"a":1}. jsonc-parser never builds a document you print. It computes a list of text edits, so the rest of the file is untouched.
const cj = require('comment-json');
const c = cj.parse(text);
c.port = 9090;
console.log(cj.stringify(c, null, 2));
const { modify, applyEdits } = require('jsonc-parser');
const edits = modify(text, ['port'], 9090, { formattingOptions: { insertSpaces: true, tabSize: 2 } });
console.log(applyEdits(text, edits));Both printed the file with only the port changed. Neither handles single quotes or unquoted keys, so for a JSON5 file with comments that must survive an edit, read Editing JSON5 Without Losing Comments.
Version, Size, License and Downloads
Every figure in this table was read from the npm registry on 12-01-2026 with npm view <package> version time dist.unpackedSize license. Downloads come from the registry's last-week endpoint for 11-24-2026 to 11-30-2026. "Published" is the publish time of that latest version; the package's own time.modified field can be later, because the registry updates it for metadata changes.
| Package | Latest version | Published | Unpacked size | License | Downloads, week of 11-24-2026 |
|---|---|---|---|---|---|
json5 | 2.2.3 | 12-31-2022 | 235,198 bytes | MIT | 302,090,972 |
jsonc-parser | 3.3.1 | 06-24-2024 | 212,821 bytes | MIT | 107,826,438 |
comment-json | 5.0.0 | 04-12-2026 | 72,700 bytes | MIT | 24,088,267 |
strip-json-comments | 5.1.0 | 11-14-2026 | 8,394 bytes | MIT | 260,347,074 |
hjson | 3.2.2 | 11-17-2020 | 124,976 bytes | MIT | 464,484 |
| Package | TypeScript types | Module format | Dependencies |
|---|---|---|---|
json5 | Yes, bundled | CommonJS entry, plus an ES module build named in the module field | None |
jsonc-parser | Yes, bundled | UMD entry, plus an ES module build named in the module field | None |
comment-json | Yes, bundled | CommonJS | array-timsort, esprima |
strip-json-comments | Yes, bundled | ES module only ("type": "module"); we loaded it with import | None |
hjson | Not bundled; types are in @types/hjson (2.4.6, read 12-01-2026) | CommonJS | None |
Unpacked size is the size of that package alone, as the registry reports it, not an install size with dependencies. Download counts measure installs, including indirect ones through other packages, so a big number says a tool is a common dependency, not that many people write its format. Of the five, only strip-json-comments and comment-json published their latest version within the 12 months before 12-01-2026.
The Five Packages in Brief
json5
The reference parser for the format this site covers. It reads the whole JSON5 grammar and writes with stringify, but writing drops comments. The latest release is dated 12-31-2022. Pin 2.2.2 or later because of CVE-2022-46175. See the JavaScript guide and the Node.js guide.
jsonc-parser
Microsoft's parser for the JSON-with-comments files that editors use. It is fault tolerant: it returns what it could read and lists the errors in an array you pass in. It can also walk a syntax tree and compute edits with modify. Use it for tsconfig.json-style files and nothing that uses single quotes or unquoted keys.
const { parse } = require('jsonc-parser');
const errors = [];
const value = parse(text, errors, { allowTrailingComma: true });
if (errors.length) throw new Error('Invalid JSONC: ' + errors.length + ' error(s)');comment-json
Parses JSON with comments into an object that remembers them, then writes them back. It is the shortest path to read, change and save a commented JSON file. In our matrix it passed comments and trailing commas and failed on single quotes, unquoted keys, hex, Infinity and line continuations. It is the only package here with runtime dependencies.
strip-json-comments
A small function that blanks out comments so JSON.parse can run. The README says it replaces comments with whitespace by default, so error positions stay close to the source, and that trailingCommas: true also removes trailing commas. It is the smallest of the five at 8,394 bytes unpacked. It is a text filter, not a parser, so the strict-JSON rules still apply to everything else.
import stripJsonComments from 'strip-json-comments';
const data = JSON.parse(stripJsonComments(text, { trailingCommas: true }));hjson
A reader and writer for Hjson, a relaxed format with #, // and /* */ comments, optional commas and quotes, and ''' multi-line strings. It reads plain JSON too, and it passed comments, trailing commas, single quotes and unquoted keys in our matrix. Read the details on JSON5 vs Hjson. Use Hjson.rt.parse and Hjson.rt.stringify when comments must survive. The last release is dated 11-17-2020.
For parsers in other languages, see the implementations list.
Which Package for Which Job
| Job | Use | Why, from our tests |
|---|---|---|
| Read a JSON5 file | json5 | Passed all nine inputs. |
Read a JSONC file such as tsconfig.json in a script | json5 or jsonc-parser | Both pass comments and trailing commas (with the option for jsonc-parser). jsonc-parser needs its errors list checked. |
| Read, change and write back commented JSON as an object | comment-json | Kept both comments on write. Strict JSON syntax plus comments and trailing commas only. |
| Change one value and keep every other byte of the file | jsonc-parser modify | Edit list left everything else untouched. |
Remove comments, then use JSON.parse | strip-json-comments | 8,394 bytes, no dependencies, ES module only. |
| A relaxed human format with comment-safe writing | hjson with Hjson.rt | Kept comments on write; a different format from JSON5. |
| Edit a JSON5 file with single quotes and keep its comments | None of these five | See preserving comments for tools that do. |
| Write a file with comments from scratch | None of these five | Only the round-trip tools keep comments that were already in the text. Build the text yourself. |
One more rule before you pick: if the file name is package.json, none of this applies. npm requires real JSON there; see comments in JSON for the options.
Common Questions
Which package is the most popular?
By the registry's last-week counts read 12-01-2026, json5 had 302,090,972 downloads, strip-json-comments 260,347,074, jsonc-parser 107,826,438, comment-json 24,088,267 and hjson 464,484. These include installs pulled in by other packages.
Can json5 read JSONC?
Yes for comments and trailing commas, which both formats allow. It also accepts more than JSONC, so it will not warn you that a file uses syntax an editor or a stricter JSONC tool rejects.
Does json5 keep comments when it writes?
No. stringify printed the data only. Use one of the editors in the preserving comments guide.
Why does hjson fail the hex and Infinity rows?
Hjson has no hex or Infinity literal. An unquoted word is a string, so on a multi-line object 0xFF came back as the text "0xFF", and on a one-line object it threw.
Are these results fixed?
They describe the exact versions named in the matrix header on 12-01-2026. A newer release may change a row, so re-run your own inputs before depending on a corner case.
Sources
(7)
- JSON5 1.0.0 specification, the syntax the matrix tests
- microsoft/node-jsonc-parser, README:
parseerrors list,modifyandapplyEdits - kaelzhang/node-comment-json, README: comments kept in symbol properties,
stringifybehavior - sindresorhus/strip-json-comments, README:
whitespaceandtrailingCommasoptions - hjson/hjson-js, README:
Hjson.rtand comment preservation - npm registry: download counts API, the
last-weekendpoint and its caveats - GitHub advisory GHSA-9c47-m6qq-7p4h, CVE-2022-46175 in
json5
Registry figures read 12-01-2026. Tested with Node.js 24.22.1 and npm 11.6.4. Last reviewed: December 1, 2026.
JavaScript
Python
TypeScript
Node.js
Rust
Java
C#
Ruby
Kotlin
Lua
Babel
Vite
webpack
ESLint
Prettier
VS Code
JetBrains