1 | # JSON5 – JSON for Humans
|
---|
2 |
|
---|
3 | [![Build Status](https://app.travis-ci.com/json5/json5.svg?branch=main)][Build
|
---|
4 | Status] [![Coverage
|
---|
5 | Status](https://coveralls.io/repos/github/json5/json5/badge.svg)][Coverage
|
---|
6 | Status]
|
---|
7 |
|
---|
8 | JSON5 is an extension to the popular [JSON] file format that aims to be
|
---|
9 | easier to **write and maintain _by hand_ (e.g. for config files)**.
|
---|
10 | It is _not intended_ to be used for machine-to-machine communication.
|
---|
11 | (Keep using JSON or other file formats for that. 🙂)
|
---|
12 |
|
---|
13 | JSON5 was started in 2012, and as of 2022, now gets **[>65M downloads/week](https://www.npmjs.com/package/json5)**,
|
---|
14 | ranks in the **[top 0.1%](https://gist.github.com/anvaka/8e8fa57c7ee1350e3491)** of the most depended-upon packages on npm,
|
---|
15 | and has been adopted by major projects like
|
---|
16 | **[Chromium](https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/runtime_enabled_features.json5;drc=5de823b36e68fd99009a29281b17bc3a1d6b329c),
|
---|
17 | [Next.js](https://github.com/vercel/next.js/blob/b88f20c90bf4659b8ad5cb2a27956005eac2c7e8/packages/next/lib/find-config.ts#L43-L46),
|
---|
18 | [Babel](https://babeljs.io/docs/en/config-files#supported-file-extensions),
|
---|
19 | [Retool](https://community.retool.com/t/i-am-attempting-to-append-several-text-fields-to-a-google-sheet-but-receiving-a-json5-invalid-character-error/7626),
|
---|
20 | [WebStorm](https://www.jetbrains.com/help/webstorm/json.html),
|
---|
21 | and [more](https://github.com/json5/json5/wiki/In-the-Wild)**.
|
---|
22 | It's also natively supported on **[Apple platforms](https://developer.apple.com/documentation/foundation/jsondecoder/3766916-allowsjson5)**
|
---|
23 | like **MacOS** and **iOS**.
|
---|
24 |
|
---|
25 | Formally, the **[JSON5 Data Interchange Format](https://spec.json5.org/)** is a superset of JSON
|
---|
26 | (so valid JSON files will always be valid JSON5 files)
|
---|
27 | that expands its syntax to include some productions from [ECMAScript 5.1] (ES5).
|
---|
28 | It's also a strict _subset_ of ES5, so valid JSON5 files will always be valid ES5.
|
---|
29 |
|
---|
30 | This JavaScript library is a reference implementation for JSON5 parsing and serialization,
|
---|
31 | and is directly used in many of the popular projects mentioned above
|
---|
32 | (where e.g. extreme performance isn't necessary),
|
---|
33 | but others have created [many other libraries](https://github.com/json5/json5/wiki/In-the-Wild)
|
---|
34 | across many other platforms.
|
---|
35 |
|
---|
36 | [Build Status]: https://app.travis-ci.com/json5/json5
|
---|
37 |
|
---|
38 | [Coverage Status]: https://coveralls.io/github/json5/json5
|
---|
39 |
|
---|
40 | [JSON]: https://tools.ietf.org/html/rfc7159
|
---|
41 |
|
---|
42 | [ECMAScript 5.1]: https://www.ecma-international.org/ecma-262/5.1/
|
---|
43 |
|
---|
44 | ## Summary of Features
|
---|
45 | The following ECMAScript 5.1 features, which are not supported in JSON, have
|
---|
46 | been extended to JSON5.
|
---|
47 |
|
---|
48 | ### Objects
|
---|
49 | - Object keys may be an ECMAScript 5.1 _[IdentifierName]_.
|
---|
50 | - Objects may have a single trailing comma.
|
---|
51 |
|
---|
52 | ### Arrays
|
---|
53 | - Arrays may have a single trailing comma.
|
---|
54 |
|
---|
55 | ### Strings
|
---|
56 | - Strings may be single quoted.
|
---|
57 | - Strings may span multiple lines by escaping new line characters.
|
---|
58 | - Strings may include character escapes.
|
---|
59 |
|
---|
60 | ### Numbers
|
---|
61 | - Numbers may be hexadecimal.
|
---|
62 | - Numbers may have a leading or trailing decimal point.
|
---|
63 | - Numbers may be [IEEE 754] positive infinity, negative infinity, and NaN.
|
---|
64 | - Numbers may begin with an explicit plus sign.
|
---|
65 |
|
---|
66 | ### Comments
|
---|
67 | - Single and multi-line comments are allowed.
|
---|
68 |
|
---|
69 | ### White Space
|
---|
70 | - Additional white space characters are allowed.
|
---|
71 |
|
---|
72 | [IdentifierName]: https://www.ecma-international.org/ecma-262/5.1/#sec-7.6
|
---|
73 |
|
---|
74 | [IEEE 754]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
|
---|
75 |
|
---|
76 | ## Example
|
---|
77 | Kitchen-sink example:
|
---|
78 |
|
---|
79 | ```js
|
---|
80 | {
|
---|
81 | // comments
|
---|
82 | unquoted: 'and you can quote me on that',
|
---|
83 | singleQuotes: 'I can use "double quotes" here',
|
---|
84 | lineBreaks: "Look, Mom! \
|
---|
85 | No \\n's!",
|
---|
86 | hexadecimal: 0xdecaf,
|
---|
87 | leadingDecimalPoint: .8675309, andTrailing: 8675309.,
|
---|
88 | positiveSign: +1,
|
---|
89 | trailingComma: 'in objects', andIn: ['arrays',],
|
---|
90 | "backwardsCompatible": "with JSON",
|
---|
91 | }
|
---|
92 | ```
|
---|
93 |
|
---|
94 | A more real-world example is [this config file](https://github.com/chromium/chromium/blob/feb3c9f670515edf9a88f185301cbd7794ee3e52/third_party/blink/renderer/platform/runtime_enabled_features.json5)
|
---|
95 | from the Chromium/Blink project.
|
---|
96 |
|
---|
97 | ## Specification
|
---|
98 | For a detailed explanation of the JSON5 format, please read the [official
|
---|
99 | specification](https://json5.github.io/json5-spec/).
|
---|
100 |
|
---|
101 | ## Installation and Usage
|
---|
102 | ### Node.js
|
---|
103 | ```sh
|
---|
104 | npm install json5
|
---|
105 | ```
|
---|
106 |
|
---|
107 | #### CommonJS
|
---|
108 | ```js
|
---|
109 | const JSON5 = require('json5')
|
---|
110 | ```
|
---|
111 |
|
---|
112 | #### Modules
|
---|
113 | ```js
|
---|
114 | import JSON5 from 'json5'
|
---|
115 | ```
|
---|
116 |
|
---|
117 | ### Browsers
|
---|
118 | #### UMD
|
---|
119 | ```html
|
---|
120 | <!-- This will create a global `JSON5` variable. -->
|
---|
121 | <script src="https://unpkg.com/json5@2/dist/index.min.js"></script>
|
---|
122 | ```
|
---|
123 |
|
---|
124 | #### Modules
|
---|
125 | ```html
|
---|
126 | <script type="module">
|
---|
127 | import JSON5 from 'https://unpkg.com/json5@2/dist/index.min.mjs'
|
---|
128 | </script>
|
---|
129 | ```
|
---|
130 |
|
---|
131 | ## API
|
---|
132 | The JSON5 API is compatible with the [JSON API].
|
---|
133 |
|
---|
134 | [JSON API]:
|
---|
135 | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON
|
---|
136 |
|
---|
137 | ### JSON5.parse()
|
---|
138 | Parses a JSON5 string, constructing the JavaScript value or object described by
|
---|
139 | the string. An optional reviver function can be provided to perform a
|
---|
140 | transformation on the resulting object before it is returned.
|
---|
141 |
|
---|
142 | #### Syntax
|
---|
143 | JSON5.parse(text[, reviver])
|
---|
144 |
|
---|
145 | #### Parameters
|
---|
146 | - `text`: The string to parse as JSON5.
|
---|
147 | - `reviver`: If a function, this prescribes how the value originally produced by
|
---|
148 | parsing is transformed, before being returned.
|
---|
149 |
|
---|
150 | #### Return value
|
---|
151 | The object corresponding to the given JSON5 text.
|
---|
152 |
|
---|
153 | ### JSON5.stringify()
|
---|
154 | Converts a JavaScript value to a JSON5 string, optionally replacing values if a
|
---|
155 | replacer function is specified, or optionally including only the specified
|
---|
156 | properties if a replacer array is specified.
|
---|
157 |
|
---|
158 | #### Syntax
|
---|
159 | JSON5.stringify(value[, replacer[, space]])
|
---|
160 | JSON5.stringify(value[, options])
|
---|
161 |
|
---|
162 | #### Parameters
|
---|
163 | - `value`: The value to convert to a JSON5 string.
|
---|
164 | - `replacer`: A function that alters the behavior of the stringification
|
---|
165 | process, or an array of String and Number objects that serve as a whitelist
|
---|
166 | for selecting/filtering the properties of the value object to be included in
|
---|
167 | the JSON5 string. If this value is null or not provided, all properties of the
|
---|
168 | object are included in the resulting JSON5 string.
|
---|
169 | - `space`: A String or Number object that's used to insert white space into the
|
---|
170 | output JSON5 string for readability purposes. If this is a Number, it
|
---|
171 | indicates the number of space characters to use as white space; this number is
|
---|
172 | capped at 10 (if it is greater, the value is just 10). Values less than 1
|
---|
173 | indicate that no space should be used. If this is a String, the string (or the
|
---|
174 | first 10 characters of the string, if it's longer than that) is used as white
|
---|
175 | space. If this parameter is not provided (or is null), no white space is used.
|
---|
176 | If white space is used, trailing commas will be used in objects and arrays.
|
---|
177 | - `options`: An object with the following properties:
|
---|
178 | - `replacer`: Same as the `replacer` parameter.
|
---|
179 | - `space`: Same as the `space` parameter.
|
---|
180 | - `quote`: A String representing the quote character to use when serializing
|
---|
181 | strings.
|
---|
182 |
|
---|
183 | #### Return value
|
---|
184 | A JSON5 string representing the value.
|
---|
185 |
|
---|
186 | ### Node.js `require()` JSON5 files
|
---|
187 | When using Node.js, you can `require()` JSON5 files by adding the following
|
---|
188 | statement.
|
---|
189 |
|
---|
190 | ```js
|
---|
191 | require('json5/lib/register')
|
---|
192 | ```
|
---|
193 |
|
---|
194 | Then you can load a JSON5 file with a Node.js `require()` statement. For
|
---|
195 | example:
|
---|
196 |
|
---|
197 | ```js
|
---|
198 | const config = require('./config.json5')
|
---|
199 | ```
|
---|
200 |
|
---|
201 | ## CLI
|
---|
202 | Since JSON is more widely used than JSON5, this package includes a CLI for
|
---|
203 | converting JSON5 to JSON and for validating the syntax of JSON5 documents.
|
---|
204 |
|
---|
205 | ### Installation
|
---|
206 | ```sh
|
---|
207 | npm install --global json5
|
---|
208 | ```
|
---|
209 |
|
---|
210 | ### Usage
|
---|
211 | ```sh
|
---|
212 | json5 [options] <file>
|
---|
213 | ```
|
---|
214 |
|
---|
215 | If `<file>` is not provided, then STDIN is used.
|
---|
216 |
|
---|
217 | #### Options:
|
---|
218 | - `-s`, `--space`: The number of spaces to indent or `t` for tabs
|
---|
219 | - `-o`, `--out-file [file]`: Output to the specified file, otherwise STDOUT
|
---|
220 | - `-v`, `--validate`: Validate JSON5 but do not output JSON
|
---|
221 | - `-V`, `--version`: Output the version number
|
---|
222 | - `-h`, `--help`: Output usage information
|
---|
223 |
|
---|
224 | ## Contributing
|
---|
225 | ### Development
|
---|
226 | ```sh
|
---|
227 | git clone https://github.com/json5/json5
|
---|
228 | cd json5
|
---|
229 | npm install
|
---|
230 | ```
|
---|
231 |
|
---|
232 | When contributing code, please write relevant tests and run `npm test` and `npm
|
---|
233 | run lint` before submitting pull requests. Please use an editor that supports
|
---|
234 | [EditorConfig](http://editorconfig.org/).
|
---|
235 |
|
---|
236 | ### Issues
|
---|
237 | To report bugs or request features regarding the JSON5 **data format**,
|
---|
238 | please submit an issue to the official
|
---|
239 | **[_specification_ repository](https://github.com/json5/json5-spec)**.
|
---|
240 |
|
---|
241 | Note that we will never add any features that make JSON5 incompatible with ES5;
|
---|
242 | that compatibility is a fundamental premise of JSON5.
|
---|
243 |
|
---|
244 | To report bugs or request features regarding this **JavaScript implementation**
|
---|
245 | of JSON5, please submit an issue to **_this_ repository**.
|
---|
246 |
|
---|
247 | ### Security Vulnerabilities and Disclosures
|
---|
248 | To report a security vulnerability, please follow the follow the guidelines
|
---|
249 | described in our [security policy](./SECURITY.md).
|
---|
250 |
|
---|
251 | ## License
|
---|
252 | MIT. See [LICENSE.md](./LICENSE.md) for details.
|
---|
253 |
|
---|
254 | ## Credits
|
---|
255 | [Aseem Kishore](https://github.com/aseemk) founded this project.
|
---|
256 | He wrote a [blog post](https://aseemk.substack.com/p/ignore-the-f-ing-haters-json5)
|
---|
257 | about the journey and lessons learned 10 years in.
|
---|
258 |
|
---|
259 | [Michael Bolin](http://bolinfest.com/) independently arrived at and published
|
---|
260 | some of these same ideas with awesome explanations and detail. Recommended
|
---|
261 | reading: [Suggested Improvements to JSON](http://bolinfest.com/essays/json.html)
|
---|
262 |
|
---|
263 | [Douglas Crockford](http://www.crockford.com/) of course designed and built
|
---|
264 | JSON, but his state machine diagrams on the [JSON website](http://json.org/), as
|
---|
265 | cheesy as it may sound, gave us motivation and confidence that building a new
|
---|
266 | parser to implement these ideas was within reach! The original
|
---|
267 | implementation of JSON5 was also modeled directly off of Doug’s open-source
|
---|
268 | [json_parse.js] parser. We’re grateful for that clean and well-documented
|
---|
269 | code.
|
---|
270 |
|
---|
271 | [json_parse.js]:
|
---|
272 | https://github.com/douglascrockford/JSON-js/blob/03157639c7a7cddd2e9f032537f346f1a87c0f6d/json_parse.js
|
---|
273 |
|
---|
274 | [Max Nanasy](https://github.com/MaxNanasy) has been an early and prolific
|
---|
275 | supporter, contributing multiple patches and ideas.
|
---|
276 |
|
---|
277 | [Andrew Eisenberg](https://github.com/aeisenberg) contributed the original
|
---|
278 | `stringify` method.
|
---|
279 |
|
---|
280 | [Jordan Tucker](https://github.com/jordanbtucker) has aligned JSON5 more closely
|
---|
281 | with ES5, wrote the official JSON5 specification, completely rewrote the
|
---|
282 | codebase from the ground up, and is actively maintaining this project.
|
---|