JSONC Simple Parser
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.
Common errors
-
SyntaxError: Unexpected token / in JSON at position X
cause Attempting to parse a JSONC string (with comments) using the native `JSON.parse()` instead of `JSONC.parse()`.fixReplace `JSON.parse(jsoncString)` with `JSONC.parse(jsoncString)`. -
TypeError: Cannot read properties of undefined (reading 'parse')
cause 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.fixEnsure 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.
cause The package is not installed, or TypeScript is unable to resolve the module path or its type definitions.fixRun `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.
Warnings
- breaking Major 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.
- gotcha The `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.
- gotcha While `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.
Install
-
npm install jsonc-simple-parser -
yarn add jsonc-simple-parser -
pnpm add jsonc-simple-parser
Imports
- JSONC
const JSONC = require('jsonc-simple-parser');import JSONC from 'jsonc-simple-parser';
- parse
import { parse } from 'jsonc-simple-parser';import JSONC from 'jsonc-simple-parser'; const parsed = JSONC.parse(source);
- stringify
import { stringify } from 'jsonc-simple-parser';import JSONC from 'jsonc-simple-parser'; const str = JSONC.stringify(data);
Quickstart
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);