JSPM

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

Try to parse a string as JSON and return undefined on failure

Package Exports

  • tryjson

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 (tryjson) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

node tryjson

tryjson

Build Status Coverage Status

This module works like JSON.parse (and in fact it uses JSON.parse) but instead of throwing exceptions it returns undefined on failure. This is not always a desired behaviour but sometimes it is.

There is also a stringify method that works like JSON.stringify but instead of throwing exceptions on circular structures it returns "null" - which, again, may not be what you always want but sometime it is and use can use this module to simplify your code in those cases.

Installation

Install to use in your Node project, updating the dependencies in package.json:

npm install tryjson --save

Examples

Basic usage:

Parsing:

var tryjson = require('tryjson');

console.log(tryjson.parse('{"a":1,"b":2}'));
// { a: 1, b: 2 }

console.log(tryjson.parse('{"a":1,"b":2'));
// undefined

Stringification:

var tryjson = require('tryjson');

var x = {a: 1};
console.log(tryjson.stringify(x));
// '{"a":1}'
x.b = x;
console.log(tryjson.stringify(x));
// 'null'

Issues

For any bug reports or feature requests please post an issue on GitHub.

Author

Rafał Pocztarski - https://github.com/rsp

License

MIT License (Expat). See LICENSE.md for details.