Everything you need in JSON land. Parse JSON with comments, stringify objects with circular references, etc...
npm install jsonc






> © 2019, Onur Yıldırım (@onury). MIT License.
Everything you need in JSON land.
npm i jsonc
- Parse JSON with comments.
- Stringify objects with circular references.
- Safely parse / stringify without try/catch blocks.
- Read and auto-parse JSON files gracefully, sync or async (with promises).
- Auto-stringify and write JSON files gracefully, sync or async (with promises).
- Strips UTF-8 BOM.
- Log objects as JSON (without worrying about errors).
- Uglify/beautify JSON strings.
- More helpful JSON errors.
- Friendly API.
- TypeScript support.
See the concise [API reference][docs-api].
``js`
const jsonc = require('jsonc');
// or
import { jsonc } from 'jsonc';
This is safe for JSON with comments:
`js`
jsonc.parse('// comment\n{"data": / comment / "value"}\n'); // » { data: 'value' }
And this is safe for circular references:
`js`
const obj = { x: 1 };
obj.y = obj; // circular
jsonc.stringify(obj); // » { x: 1, y: '[Circular]' }
But this is seriously safe:
`js
// safe version of every method
const jsonc = require('jsonc').safe;
// or
import { safe as jsonc } from 'jsonc';
const [err, result] = jsonc.parse('[invalid JSON}');
if (err) {
console.log(Failed to parse JSON: ${err.message});`
} else {
console.log(result);
}
See the concise [API reference][docs-api].
- v2.0.0 (2019-06-17)
+ Requires Node.js v8 or newer.
+ Updated dependencies.
- v1.1.0 (2018-11-22)
+ Fixed an issue where TypeScript compiler would complain about 'declare' modifier`.
+ Improved typings for safe methods.
+ Updated core dependencies.
- v1.0.0 (2018-10-18)
+ Initial release.
[docs-api]:https://onury.io/jsonc/api
[strip-json-comments]:https://github.com/sindresorhus/strip-json-comments
[json-stringify-safe]:https://github.com/isaacs/json-stringify-safe
[parse-json]:https://github.com/sindresorhus/parse-json
[fs-extra]:https://www.npmjs.com/package/fs-extra