You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # parse-json [![Build Status](https://travis-ci.org/sindresorhus/parse-json.svg?branch=master)](https://travis-ci.org/sindresorhus/parse-json)
  2. > Parse JSON with more helpful errors
  3. ## Install
  4. ```
  5. $ npm install --save parse-json
  6. ```
  7. ## Usage
  8. ```js
  9. var parseJson = require('parse-json');
  10. var json = '{\n\t"foo": true,\n}';
  11. JSON.parse(json);
  12. /*
  13. undefined:3
  14. }
  15. ^
  16. SyntaxError: Unexpected token }
  17. */
  18. parseJson(json);
  19. /*
  20. JSONError: Trailing comma in object at 3:1
  21. }
  22. ^
  23. */
  24. parseJson(json, 'foo.json');
  25. /*
  26. JSONError: Trailing comma in object at 3:1 in foo.json
  27. }
  28. ^
  29. */
  30. // you can also add the filename at a later point
  31. try {
  32. parseJson(json);
  33. } catch (err) {
  34. err.fileName = 'foo.json';
  35. throw err;
  36. }
  37. /*
  38. JSONError: Trailing comma in object at 3:1 in foo.json
  39. }
  40. ^
  41. */
  42. ```
  43. ## API
  44. ### parseJson(input, [reviver], [filename])
  45. #### input
  46. Type: `string`
  47. #### reviver
  48. Type: `function`
  49. 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
  50. ) for more.
  51. #### filename
  52. Type: `string`
  53. Filename displayed in the error message.
  54. ## License
  55. MIT © [Sindre Sorhus](http://sindresorhus.com)