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

Five package boxes on a bench being tested against the same set of JSON inputs

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.

Table icon

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.

Inputjson5 2.2.3jsonc-parser 3.3.1comment-json 5.0.0strip-json-comments 5.1.0hjson 3.2.2JSON.parse
Strict JSONYesYesYesYesYesYes
Line comment //YesYesYesYesYesNo
Block comment /* */YesYesYesYesYesNo
Trailing commaYesNo (Yes with allowTrailingComma)YesNo (Yes with trailingCommas)YesNo
Single-quoted stringYesNoNoNoYesNo
Unquoted keyYesNoNoNoYesNo
Hex number 0xFFYesNoNoNoNo (reads a string or throws)No
InfinityYesNoNoNoNo (reads a string or throws)No
Backslash line continuation in a stringYesNoNoNoNoNo

Reading the table:

  • Only json5 passed 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-parser failed them by default and passed with allowTrailingComma: true. strip-json-comments failed them by default and passed with trailingCommas: true. Every other row failed even with those options on.
  • jsonc-parser returns a value even when it fails. For {a:1} it reported errors and returned {}; for a hex number it returned 0. If you skip the errors list you get quietly wrong data.
  • hjson parses hex and Infinity into strings. On a multi-line object, a: 0xFF gave {"a":"0xFF"} and a: Infinity gave {"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.
Refresh icon

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" }
PackageHow it writesComments after the edit
json5JSON5.stringify(object)Lost. Output was { port: 9090, name: 'demo', } across three lines, in JSON5 style.
jsonc-parsermodify returns edits, applyEdits applies them. No object writer.Kept. Only 8080 changed; both comments stayed where they were.
comment-jsonstringify(object, null, 2)Kept. Both comments were written back in place.
strip-json-commentsCannot write. It returns text with the comments replaced by spaces.Removed on purpose.
hjsonHjson.stringify(object), or Hjson.rt.stringify for round tripsstringify 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.

Database icon

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.

PackageLatest versionPublishedUnpacked sizeLicenseDownloads, week of 11-24-2026
json52.2.312-31-2022235,198 bytesMIT302,090,972
jsonc-parser3.3.106-24-2024212,821 bytesMIT107,826,438
comment-json5.0.004-12-202672,700 bytesMIT24,088,267
strip-json-comments5.1.011-14-20268,394 bytesMIT260,347,074
hjson3.2.211-17-2020124,976 bytesMIT464,484
PackageTypeScript typesModule formatDependencies
json5Yes, bundledCommonJS entry, plus an ES module build named in the module fieldNone
jsonc-parserYes, bundledUMD entry, plus an ES module build named in the module fieldNone
comment-jsonYes, bundledCommonJSarray-timsort, esprima
strip-json-commentsYes, bundledES module only ("type": "module"); we loaded it with importNone
hjsonNot bundled; types are in @types/hjson (2.4.6, read 12-01-2026)CommonJSNone

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.

Node.js logo

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.

Target icon

Which Package for Which Job

JobUseWhy, from our tests
Read a JSON5 filejson5Passed all nine inputs.
Read a JSONC file such as tsconfig.json in a scriptjson5 or jsonc-parserBoth 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 objectcomment-jsonKept both comments on write. Strict JSON syntax plus comments and trailing commas only.
Change one value and keep every other byte of the filejsonc-parser modifyEdit list left everything else untouched.
Remove comments, then use JSON.parsestrip-json-comments8,394 bytes, no dependencies, ES module only.
A relaxed human format with comment-safe writinghjson with Hjson.rtKept comments on write; a different format from JSON5.
Edit a JSON5 file with single quotes and keep its commentsNone of these fiveSee preserving comments for tools that do.
Write a file with comments from scratchNone of these fiveOnly 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.

Question icon

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)
  1. JSON5 1.0.0 specification, the syntax the matrix tests
  2. microsoft/node-jsonc-parser, README: parse errors list, modify and applyEdits
  3. kaelzhang/node-comment-json, README: comments kept in symbol properties, stringify behavior
  4. sindresorhus/strip-json-comments, README: whitespace and trailingCommas options
  5. hjson/hjson-js, README: Hjson.rt and comment preservation
  6. npm registry: download counts API, the last-week endpoint and its caveats
  7. 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.