Initial commit
[yaffs-website] / node_modules / parse-json / readme.md
1 # parse-json [![Build Status](https://travis-ci.org/sindresorhus/parse-json.svg?branch=master)](https://travis-ci.org/sindresorhus/parse-json)
2
3 > Parse JSON with more helpful errors
4
5
6 ## Install
7
8 ```
9 $ npm install --save parse-json
10 ```
11
12
13 ## Usage
14
15 ```js
16 var parseJson = require('parse-json');
17 var json = '{\n\t"foo": true,\n}';
18
19
20 JSON.parse(json);
21 /*
22 undefined:3
23 }
24 ^
25 SyntaxError: Unexpected token }
26 */
27
28
29 parseJson(json);
30 /*
31 JSONError: Trailing comma in object at 3:1
32 }
33 ^
34 */
35
36
37 parseJson(json, 'foo.json');
38 /*
39 JSONError: Trailing comma in object at 3:1 in foo.json
40 }
41 ^
42 */
43
44
45 // you can also add the filename at a later point
46 try {
47         parseJson(json);
48 } catch (err) {
49         err.fileName = 'foo.json';
50         throw err;
51 }
52 /*
53 JSONError: Trailing comma in object at 3:1 in foo.json
54 }
55 ^
56 */
57 ```
58
59 ## API
60
61 ### parseJson(input, [reviver], [filename])
62
63 #### input
64
65 Type: `string`
66
67 #### reviver
68
69 Type: `function`
70
71 Prescribes how the value originally produced by parsing is transformed, before being returned. See [`JSON.parse` docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter
72 ) for more.
73
74 #### filename
75
76 Type: `string`
77
78 Filename displayed in the error message.
79
80
81 ## License
82
83 MIT © [Sindre Sorhus](http://sindresorhus.com)