Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

This package has been deprecated

Author message:

Use fs-extra or async/await + util.promisify or fs.promises

try-read-json

2.0.0 • Public • Published

try-read-json npmjs.com The MIT License npm downloads

Graceful reading of JSON value, using JSON.parse with support for optional callback

code climate standard code style travis build status coverage status dependency status

Install

Install with npm

$ npm i try-read-json --save

Usage

For more use-cases see the tests

const tryReadJson = require('try-read-json')

API

tryReadJson

Parses input JSON value without throwing an errors.

Params

  • input {String|Number|Null|Boolean}: json value
  • callback {Function}: optional callback
  • returns {Undefined|Error}: if something fails and there's no callback it returns that Error

Example

var tryReadJson = require('try-read-json')
 
// synchronous
console.log(tryReadJson(1234)) // => 1234
console.log(tryReadJson('1234')) // => 1234
console.log(tryReadJson('["aaa", "bbb"]')) // => [ 'aaa', 'bbb' ]
console.log(tryReadJson('{"foo": "bar"}')) // => { foo: 'bar' }
console.log(tryReadJson(null)) // => null
console.log(tryReadJson(true)) // => true
console.log(tryReadJson(false)) // => false
 
console.log(tryReadJson()) // => SyntaxError
console.log(tryReadJson(undefined)) // => SyntaxError
console.log(tryReadJson('{"foo:bxbba')) // => SyntaxError
 
// with callback
tryReadJson('{"foo":"bar"}', function cb (err, obj) {
  console.log(err, obj) // => null, { foo: 'bar' }
})
 
tryReadJson(123, function cb (err, num) {
  console.log(err, num) // => null, 123
})
 
tryReadJson('["aaa", "bbb"]', function cb (err, arr) {
  console.log(err, arr) // => null, [ 'aaa', 'bbb' ]
  console.log(arr[0]) // => 'aaa'
  console.log(arr[1]) // => 'bbb'
})
 
tryReadJson('{foo fail', function cb (err) {
  console.log(err) // => SyntaxError
})
 
tryReadJson(undefined, function cb (err) {
  console.log(err) // => SyntaxError
})
 
tryReadJson(true, function cb (err, bool) {
  console.log(err, bool) // => null, true
})

Related

  • always-done: Handle completion and errors with elegance! Support for streams, callbacks, promises, child processes, async/await and sync functions. A drop-in replacement for [async-done… more | homepage
  • then-parse-json: Gracefully parse JSON using promises - promisified JSON.parse | homepage
  • to-callback: Converts result-first callbacks to classic (node.js-style) error-first callbacks with 3 lines of code. Useful when you want to promisify result-first APIs (like… more | homepage
  • try-catch-callback: try/catch block with a callback, used in try-catch-core. Use it when you don't care about asyncness so much and don't want guarantees… more | homepage
  • try-catch-core: Low-level package to handle completion and errors of sync or asynchronous functions, using once and dezalgo libs. Useful for and used in… more | homepage
  • write-file: Writing a file to disk, creates intermediate directories in the destination path if they dont already exist. | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github

Install

npm i try-read-json

DownloadsWeekly Downloads

3

Version

2.0.0

License

MIT

Last publish

Collaborators

  • avatar
  • avatar