source: trip-planner-front/node_modules/parse-json/readme.md@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.1 KB
Line 
1# parse-json
2
3> Parse JSON with more helpful errors
4
5## Install
6
7```
8$ npm install parse-json
9```
10
11## Usage
12
13```js
14const parseJson = require('parse-json');
15
16const json = '{\n\t"foo": true,\n}';
17
18
19JSON.parse(json);
20/*
21undefined:3
22}
23^
24SyntaxError: Unexpected token }
25*/
26
27
28parseJson(json);
29/*
30JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}'
31
32 1 | {
33 2 | "foo": true,
34> 3 | }
35 | ^
36*/
37
38
39parseJson(json, 'foo.json');
40/*
41JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
42
43 1 | {
44 2 | "foo": true,
45> 3 | }
46 | ^
47*/
48
49
50// You can also add the filename at a later point
51try {
52 parseJson(json);
53} catch (error) {
54 if (error instanceof parseJson.JSONError) {
55 error.fileName = 'foo.json';
56 }
57
58 throw error;
59}
60/*
61JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
62
63 1 | {
64 2 | "foo": true,
65> 3 | }
66 | ^
67*/
68```
69
70## API
71
72### parseJson(string, reviver?, filename?)
73
74Throws a `JSONError` when there is a parsing error.
75
76#### string
77
78Type: `string`
79
80#### reviver
81
82Type: `Function`
83
84Prescribes 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
85) for more.
86
87#### filename
88
89Type: `string`
90
91Filename displayed in the error message.
92
93### parseJson.JSONError
94
95Exposed for `instanceof` checking.
96
97#### fileName
98
99Type: `string`
100
101The filename displayed in the error message.
102
103#### codeFrame
104
105Type: `string`
106
107The printable section of the JSON which produces the error.
108
109---
110
111<div align="center">
112 <b>
113 <a href="https://tidelift.com/subscription/pkg/npm-parse-json?utm_source=npm-parse-json&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
114 </b>
115 <br>
116 <sub>
117 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
118 </sub>
119</div>
Note: See TracBrowser for help on using the repository browser.