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