jsontryparse

Handy & Dead Simple JSON Parser

jsontryparse

Current Version Build Status Coverage Status

Handy & Dead Simple JSON Parser

Installation

$ npm install jsontryparse

or

$ yarn add jsontryparse

Usage

Direct

var json = require("jsontryparse");

var goodStr = '{"hello": "world"}',
    badStr = '{"hello"x: "world"}';

var goodObj = json.parse(goodStr),
    badObj = json.parse(badStr);

console.log("good =>", goodObj);
console.log("bad =>", badObj);

// OUTPUT
// good => { hello: 'world' }
// bad => undefined

With (err, obj) callback

var json = require("jsontryparse");

var goodStr = '{"hello": "world"}',
    badStr = '{"hello"x: "world"}';

json.tryParse(goodStr, (err, obj) => {
    if (err) {
        console.log("good (error) =>", err);
    }
    else {
        console.log("good (success) =>", obj);
    }
});

json.tryParse(badStr, (err, obj) => {
    if (err) {
        console.log("bad (error) =>", err);
    }
    else {
        console.log("bad (success) =>", obj);
    }
});

// OUTPUT
// good (success) => { hello: 'world' }
// bad (error) => SyntaxError: Unexpected token x in JSON at position 8
// ...

Tests

$ npm test

or

$ yarn test

License

MIT

Related Repositories

jsontryparse

jsontryparse

Handy & Dead Simple JSON Parser ...