JSONC Simple Parser

3.0.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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);

view raw JSON →