Registry / serialization / tiny-jsonc

tiny-jsonc

JSON →
library1.0.2jsnpmunverified

tiny-jsonc is an extremely lightweight JavaScript and TypeScript library designed specifically for parsing JSONC (JSON with Comments) strings. Currently at version 1.0.2, it employs a minimal, regex-based approach to first strip comments and then trailing commas from a JSONC input string. The cleaned string is then passed directly to the native `JSON.parse` function for final object instantiation. This method prioritizes small bundle size and operational simplicity, making it particularly suitable for environments where minimizing dependencies and code footprint is critical. It does not offer advanced JSONC manipulation APIs, rich error messages, or comprehensive parsing features found in full-blown parsers. The library's stable nature and focused scope suggest a maintenance-oriented release cadence, emphasizing reliability over rapid feature development. Developers needing more robust error handling or advanced parsing capabilities are explicitly directed to alternatives like `jsonc-simple-parser`.

npm install tiny-jsonc
INSTALL
IMPORT
SIG · TINY-JSONC
T
tiny-jsonc
serializationjavascriptv1.0.2
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

JSONC
✓ import JSONC from 'tiny-jsonc';
✗ import { JSONC } from 'tiny-jsonc';
The library's main functionality is exposed as a default export named `JSONC`, which contains the `parse` method. It is primarily designed for ES Module environments and ships with TypeScript types.
JSONC.parse
✓ import JSONC from 'tiny-jsonc'; const parsed = JSONC.parse(source);
✗ const JSONC = require('tiny-jsonc'); const parsed = JSONC.parse(source);
While CommonJS `require` might work with module interop, the library's documentation and intended usage patterns favor ES module `import` statements.

This quickstart demonstrates how to import and use `tiny-jsonc` to parse a JSONC string containing both single-line and multi-line comments, as well as trailing commas, into a standard JavaScript object.

import JSONC from 'tiny-jsonc'; const sourceWithCommentsAndTrailingCommas = ` { // This is an example comment "productName": "Example Widget", "price": 123.45, /* This is a multi-line comment */ "features": [ "featureA", "featureB", // Inline comment "featureC", ], "metadata": { "id": "abc-123", "timestamp": 1678886400000, }, // Trailing comma on object property } `; try { const parsedObject = JSONC.parse(sourceWithCommentsAndTrailingCommas); console.log('Successfully parsed JSONC:'); console.log(JSON.stringify(parsedObject, null, 2)); // Expected output will be valid JSON without comments and trailing commas // e.g., { "productName": "Example Widget", "price": 123.45, "features": [ "featureA", "featureB", "featureC" ], "metadata": { "id": "abc-123", "timestamp": 1678886400000 } } } catch (error) { console.error('Failed to parse JSONC:', error.message); }
Debug
Known issues
gotchaThis library is intentionally minimalistic and not a 'full-blown' parser. It relies on simple regexes to strip comments and trailing commas before passing the result to `JSON.parse`. For advanced JSONC manipulation, better error messages, or comprehensive parsing, consider libraries like `jsonc-simple-parser`.
fix
Evaluate your parsing requirements. If you need robust error reporting, AST manipulation, or to handle highly unconventional JSONC structures, opt for a more feature-rich parser.
affects: >=1.0.0
gotchaAfter `tiny-jsonc` performs its initial stripping of comments and trailing commas, the resulting string is parsed by the native `JSON.parse` function. This means that the library inherits all the strictness and limitations of standard JSON parsing regarding other syntax rules (e.g., specific string escaping, number formats).
fix
Ensure that the JSON content, *after* comments and trailing commas are removed, conforms strictly to the JSON specification (RFC 8259). Any non-standard JSON syntax will result in `JSON.parse` errors.
affects: >=1.0.0
gotchaThe regex-based stripping mechanism may not perfectly handle all highly unusual, deeply nested, or extremely malformed JSONC comment edge cases. While effective for common patterns, complex or ambiguous comment placements could potentially lead to unexpected parsing failures or incorrect output compared to a full, tokenizing parser.
fix
If encountering unexpected parsing errors with complex JSONC structures, verify the input format. For maximum robustness with highly varied or potentially malformed inputs, a more sophisticated parser that builds an Abstract Syntax Tree (AST) might be necessary.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Unexpected token / in JSON at position X
The `tiny-jsonc` parser failed to correctly strip a comment from the input string, leading `JSON.parse` to encounter a comment delimiter (like `//` or `/*`) which is invalid in standard JSON.
fix
Inspect the JSONC input around the indicated position (X) for unusual comment formatting or edge cases that the regex might miss. Ensure comments follow standard JSONC syntax (`//` for line, `/* */` for block comments).
SyntaxError: Unexpected token , in JSON at position X
A comma was present in the JSONC input at a position where `JSON.parse` does not expect it (e.g., not as a separator between object properties or array elements). This typically occurs if `tiny-jsonc` didn't strip a trailing comma, or if there's an incorrectly placed comma that isn't a trailing comma.
fix
Verify the JSONC input around the indicated position for correctly placed commas. `tiny-jsonc` specifically targets *trailing* commas; other misplaced commas will still cause errors from `JSON.parse`.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
tiny-jsonc — npm install tiny-jsonc · libregistry