parse-json
Parse JSON with more helpful errors
Install
$ npm install --save parse-json
Usage
const parseJson = ;const json = '{\n\t"foo": true,\n}'; JSON;/*undefined:3}^SyntaxError: Unexpected token }*/ ;/*JSONError: Trailing comma in object at 3:1}^*/ ;/*JSONError: Trailing comma in object at 3:1 in foo.json}^*/ // you can also add the filename at a later pointtry ; catch err errfileName = 'foo.json'; throw err;/*JSONError: Trailing comma in object at 3:1 in foo.json}^*/
API
parseJson(input, [reviver], [filename])
input
Type: string
reviver
Type: Function
Prescribes how the value originally produced by parsing is transformed, before being returned. See JSON.parse
docs for more.
filename
Type: string
Filename displayed in the error message.
License
MIT © Sindre Sorhus