json-parse-even-better-errors
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The npm package "json-parse-even-better-errors" is a Node.js library that improves the functionality of JSON.parse()
. It provides more informative and easier to understand error messages which include the context and location of parse errors. It also takes care to preserve the newline and indentation styles present in the JSON data. These styles are kept in the object or array in Symbol.for('indent')
and Symbol.for('newline')
properties. This package can be particularly useful for developers who work with JSON data frequently and need a better way to handle and decipher errors.
Here's how you can start using json-parse-even-better-errors. First, you need to install the package:
$ npm install --save json-parse-even-better-errors
Then, you can require it in your code like so:
const parseJson = require('json-parse-even-better-errors')
You can use it to parse JSON in a way similar to the JSON.parse()
method, but with better errors:
parseJson('"foo"') // returns the string 'foo'
parseJson('garbage') // this will throw a more informative error message
The package also provides a noExceptions
method that returns undefined
instead of throwing an error :
parseJson.noExceptions('garbage') // returns undefined
If you want to preserve JSON data indentation and newline character(s) when saving the file back to disk, you could use the following code:
const txt = await readFile('./package.json', 'utf8')
const data = parseJsonEvenBetterErrors(txt)
const indent = Symbol.for('indent')
const newline = Symbol.for('newline')
// .. do some stuff to the data ..
const string = JSON.stringify(data, null, data[indent]) + '\n'
const eolFixed = data[newline] === '\n' ? string
: string.replace(/\n/g, data[newline])
await writeFile('./package.json', eolFixed)
The documentation for json-parse-even-better-errors can be found in its GitHub repository. The readme file in the repository provides a detailed explanation of the package, including installation instructions, features, example usage, and API documentation.