better-error-message-for-json-parse
TypeScript icon, indicating that this package has built-in type declarations

0.1.6 • Public • Published

Safe JSON Parse

The main goal of this project is to provide a nice error message for the SyntaxError that may occur while parsing JSON data.

For example, parsing the following JSON:

{
  "index": 1,
  "index_start_at": 56,
  "integer": 6,
  "float": 11.6816,
  "name": "Eva",
  "surname" "Rowland",
  "fullname": "Denise Pickett",
  "email": "jack@garrison.vc",
  "bool": false
}

will result in the following error message:

Unexpected string in JSON at position 105
  "name": "Eva",
  "surname" "Rowland",
-----------^
  "fullname": "Denise Pickett"

How to use

  1. import it
var { safeJsonParse } = require('better-error-message-for-json-parse');
  1. use it
try {
  safeJsonParse('{"a": 2, "b": 3, "v" 3, "e": 4 }');
} catch (e) {
  console.log(e);
}
  1. enjoy:
$ node index.js
SyntaxError: Unexpected number in JSON at position 21
{"a": 2, "b": 3, "v" 3, "e": 4 }
--------------------^
at exports.safeJsonParse

Package Sidebar

Install

npm i better-error-message-for-json-parse

Weekly Downloads

97

Version

0.1.6

License

MIT

Unpacked Size

10.6 kB

Total Files

12

Last publish

Collaborators

  • raqystyle