Registry / serialization / jsonc-simple-parser

jsonc-simple-parser

JSON →
library3.0.0jsnpmunverified

jsonc-simple-parser is a lightweight and performant JavaScript/TypeScript library designed to parse JSON-like data that includes comments (JSONC) and optional trailing commas. Currently stable at version 3.0.0, this package stands out for its minimal bundle size, being approximately 3.5kb minified and gzipped (or ~2kb if `RegHex` is already in your bundle). It boasts exceptional performance, often being 10x faster than VS Code's `jsonc-parser` and 70x faster than `JSON5` for standard JSON, and equally fast for JSONC. The library prioritizes correctness by passing all 274 JSON tests from ECMA's test262 suite, ensuring reliable parsing despite its non-spec-compliant nature (as standard JSON does not allow comments or trailing commas). It ships with built-in TypeScript types for an enhanced developer experience.

npm install jsonc-simple-parser
INSTALL
IMPORT
SIG · JSONC-SIMPLE-PARSE
J
jsonc-simple-parser
serializationjavascriptv3.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

JSONC
import JSONC from 'jsonc-simple-parser';
const JSONC = require('jsonc-simple-parser');
The primary API is exposed as a default export, primarily designed for ESM. Direct CommonJS `require` might lead to accessing `.default`.
parse
import JSONC from 'jsonc-simple-parser'; const parsed = JSONC.parse(source);
import { parse } from 'jsonc-simple-parser';
The `parse` function is a method of the default `JSONC` export, not a named export itself.
stringify
import JSONC from 'jsonc-simple-parser'; const str = JSONC.stringify(data);
import { stringify } from 'jsonc-simple-parser';
Like `parse`, `stringify` is a method of the default `JSONC` export. It behaves identically to `JSON.stringify`.

Demonstrates parsing a JSONC string containing comments and trailing commas, and then stringifying a plain object back to standard JSON.

import JSONC from 'jsonc-simple-parser'; const source = ` { // This is an example comment "name": "Alice", "age": 30, /* This is another comment */ "tags": ["developer", "js", "ts",], } `; // Parse the JSONC string into a JavaScript object const parsedObject = JSONC.parse(source); console.log('Parsed Object:', parsedObject); /* Expected output: { name: 'Alice', age: 30, tags: [ 'developer', 'js', 'ts' ] } */ // Stringify a JavaScript object back to a JSON string (comments will be lost) const dataToSave = { userId: 123, settings: { theme: 'dark' } }; const jsonString = JSONC.stringify(dataToSave, null, 2); console.log('Stringified JSON:', jsonString);
Debug
Known issues
breakingMajor version 3.0.0 was released, which typically indicates breaking changes. While the README does not explicitly list them, users should consult the package's GitHub releases or changelog for specific migration details.
fix
Review the official changelog or release notes for `jsonc-simple-parser` v3.0.0 on its GitHub repository to understand any API changes or behavioral modifications and update your code accordingly.
affects: >=3.0.0
gotchaThe `JSONC.stringify()` method behaves identically to the native `JSON.stringify()`. This means that any comments or trailing commas that were present in the original JSONC string will be removed during the stringification process.
fix
If you need to preserve comments or trailing commas, `jsonc-simple-parser` does not provide this functionality on stringification. Consider alternative approaches or other libraries if comment preservation is a requirement for output.
affects: >=1.0.0
gotchaWhile `jsonc-simple-parser` correctly parses JSON with comments and trailing commas, it is not strictly compliant with the ECMA-404 JSON specification, as the native JSON spec does not allow these features. This is by design, as it implements the 'JSONC' (JSON with Comments) standard.
fix
Be aware that output generated by `JSONC.stringify` (which is standard JSON) will lose these non-standard features. Ensure downstream systems expecting strict JSON will not encounter issues if you manually re-introduce comments for human readability.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Unexpected token / in JSON at position X
Attempting to parse a JSONC string (with comments) using the native `JSON.parse()` instead of `JSONC.parse()`.
fix
Replace `JSON.parse(jsoncString)` with `JSONC.parse(jsoncString)`.
TypeError: Cannot read properties of undefined (reading 'parse')
Incorrectly importing or accessing the `parse` method, often when trying to use CommonJS `require` with an ESM-first default export without accessing the `.default` property.
fix
Ensure you are using `import JSONC from 'jsonc-simple-parser';` for ESM, or if in CommonJS, try `const JSONC = require('jsonc-simple-parser').default;` (though ESM `import` is recommended).
TS2307: Cannot find module 'jsonc-simple-parser' or its corresponding type declarations.
The package is not installed, or TypeScript is unable to resolve the module path or its type definitions.
fix
Run `npm install jsonc-simple-parser` or `yarn add jsonc-simple-parser`. Ensure your `tsconfig.json` has `moduleResolution` set appropriately (e.g., `bundler` or `node16`) and that the module is correctly referenced in your import statements.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
jsonc-simple-parser — npm install jsonc-simple-parser · libregistry