UNPKG

1.53 kBMarkdownView Raw
1# strip-json-comments
2
3> Strip comments from JSON. Lets you use comments in your JSON files!
4
5This is now possible:
6
7```js
8{
9 // Rainbows
10 "unicorn": /* ❤ */ "cake"
11}
12```
13
14It 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.
15
16Also 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.
17
18## Install
19
20```sh
21npm install strip-json-comments
22```
23
24## Usage
25
26```js
27import stripJsonComments from 'strip-json-comments';
28
29const json = `{
30 // Rainbows
31 "unicorn": /* ❤ */ "cake"
32}`;
33
34JSON.parse(stripJsonComments(json));
35//=> {unicorn: 'cake'}
36```
37
38## API
39
40### stripJsonComments(jsonString, options?)
41
42#### jsonString
43
44Type: `string`
45
46Accepts a string with JSON and returns a string without comments.
47
48#### options
49
50Type: `object`
51
52##### trailingCommas
53
54Type: `boolean`\
55Default: `false`
56
57Strip trailing commas in addition to comments.
58
59##### whitespace
60
61Type: `boolean`\
62Default: `true`
63
64Replace comments and trailing commas with whitespace instead of stripping them entirely.
65
66## Benchmark
67
68```sh
69npm run bench
70```
71
72## Related
73
74- [strip-json-comments-cli](https://github.com/sindresorhus/strip-json-comments-cli) - CLI for this module
75- [strip-css-comments](https://github.com/sindresorhus/strip-css-comments) - Strip comments from CSS