JSPM

try-json-parse

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5322
  • Score
    100M100P100Q129159F
  • License MIT

Doesn't throw an error when JSON.parse() fails, just returns null

Package Exports

  • try-json-parse

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (try-json-parse) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

try-json-parse

Just like JSON.parse(), except it returns null instead of throwing an error for invalid JSON.

Usage

var JSONparse = require('./index'),
    assert    = require('assert'),
    fs        = require('fs');

fs.readFile(__dirname + '/package.json', function(err, content) {
  var pkgJson;

  if (err) { throw err; }

  pkgJson = JSONparse(content);
  assert.equal(pkgJson.name, 'try-json-parse');
  assert.equal(JSONparse(fs.readFileSync(__filename)), null);
});

Motivation

Got tired of writing try {} catch {}.

License

MIT