Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/strip-json-comments
Strip comments from JSON. Lets you use comments in your JSON files!
https://github.com/sindresorhus/strip-json-comments
json json-comments jsonc npm-package
Last synced: about 1 month ago
JSON representation
Strip comments from JSON. Lets you use comments in your JSON files!
- Host: GitHub
- URL: https://github.com/sindresorhus/strip-json-comments
- Owner: sindresorhus
- License: mit
- Created: 2013-11-17T15:04:17.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2023-07-04T11:22:45.000Z (over 1 year ago)
- Last Synced: 2024-04-14T11:09:17.066Z (7 months ago)
- Topics: json, json-comments, jsonc, npm-package
- Language: JavaScript
- Homepage:
- Size: 146 KB
- Stars: 589
- Watchers: 15
- Forks: 55
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome-nodejs-cn - strip-json-comments - JSON 去除注释 (包 / 解析)
- awesome-nodejs - strip-json-comments - Strip comments from JSON. Lets you use comments in your JSON files. ![](https://img.shields.io/github/stars/sindresorhus/strip-json-comments.svg?style=social&label=Star) (Repository / Object / JSON / JSON Schema)
- awesome-nodejs-cn - strip-json-comments - **star:592** 从JSON中删除注释 (包 / 解析)
- awesome-nodejs - strip-json-comments - Strip comments from JSON. (Packages / Parsing)
- awesome-nodejs - strip-json-comments - Strip comments from JSON. Lets you use comments in your JSON files! - ★ 381 (Parsing)
- awesome-node - strip-json-comments - Strip comments from JSON. (Packages / Parsing)
- awesome-nodejs-cn - strip-json-comments - JSON注释剔除工具. (目录 / 解析工具)
README
# strip-json-comments
> Strip comments from JSON. Lets you use comments in your JSON files!
This is now possible:
```js
{
// Rainbows
"unicorn": /* ❤ */ "cake"
}
```It will replace single-line comments `//` and multi-line comments `/**/` with whitespace. This allows JSON error positions to remain as close as possible to the original source.
Also available as a [Gulp](https://github.com/sindresorhus/gulp-strip-json-comments)/[Grunt](https://github.com/sindresorhus/grunt-strip-json-comments)/[Broccoli](https://github.com/sindresorhus/broccoli-strip-json-comments) plugin.
## Install
```sh
npm install strip-json-comments
```## Usage
```js
import stripJsonComments from 'strip-json-comments';const json = `{
// Rainbows
"unicorn": /* ❤ */ "cake"
}`;JSON.parse(stripJsonComments(json));
//=> {unicorn: 'cake'}
```## API
### stripJsonComments(jsonString, options?)
#### jsonString
Type: `string`
Accepts a string with JSON and returns a string without comments.
#### options
Type: `object`
##### trailingCommas
Type: `boolean`\
Default: `false`Strip trailing commas in addition to comments.
##### whitespace
Type: `boolean`\
Default: `true`Replace comments and trailing commas with whitespace instead of stripping them entirely.
## Benchmark
```sh
npm run bench
```## Related
- [strip-json-comments-cli](https://github.com/sindresorhus/strip-json-comments-cli) - CLI for this module
- [strip-css-comments](https://github.com/sindresorhus/strip-css-comments) - Strip comments from CSS