Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-parse-safe

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-parse-safe

Parse your json safely and stop writing try {} catch {}

  • 1.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.4K
decreased by-11.77%
Maintainers
1
Weekly downloads
 
Created
Source

json-parse-safe

Parse your json safely and stop writing try {} catch {}

Build StatusCode Coverage 100%ISC License

Syntax

var JSONParse = require('json-parse-safe')
JSONParse(text[, reviver])

Parameters

text
  • the string to parse as JSON
reviver (optional)
  • if a function, prescribes how the value originally produced by parsing is transformed, before being returned, more info about this param in here

Returns

object {value, error}
  • value: should be the object corresponding to the given JSON text or undefined in case of error
  • error: should be an Error object in case of error or undefined in case of success

Example

var JSONParse = require('json-parse-safe');

var str = '{"cat": "Peter", "age": 1, "colors": ["white", "cyan", "black"]}';

var obj = JSONParse(str);
// obj.value should be
{
    cat: 'Peter',
    age: 1,
    colors: ['white', 'cyan', 'black']
}

getting the exception

var bad = '{"cat": "Peter" "age": 1}';

var obj = JSONParse(str);

// obj.value should be 'undefined'
// obj.error should be an Error
console.log(obj.error);
console.log(obj.error.message);
console.log(obj.error.stack);

Development

This project has been set up with a precommit that forces you to follow a code style, no jshint issues and 100% of code coverage before commit

to run test

npm test

to run jshint

npm run jshint

to run code style

npm run code-style

to run check code coverage

npm run check-coverage

to open the code coverage report

npm run open-coverage

Keywords

FAQs

Package last updated on 14 Nov 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc