Socket
Socket
Sign inDemoInstall

easy-json-parse

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    easy-json-parse

Parse your json safely and easily.


Version published
Maintainers
1
Install size
49.7 kB
Created

Readme

Source

easy-json-parse

Are you still worried about the long code with try {} catch {}? like this:

const jsonString = 'easy';
let json;

try {
  json = JSON.parse(jsonString);
} catch (e) {
  json = {};
}

console.log(json.xxx);

or

const jsonString = null;
let json;

try {
  json = JSON.parse(jsonString) || {};
} catch (e) {
  json = {};
}

console.log(json.xxx);

Now, no matter whether it is long code or initialization, you don't need to worry.

use easy-json-parse will more easy and safe for try{} catch{}.

Get started

npm i easy-json-parse

case: initialValue

import easyParse from 'easy-json-parse';
const jsonString = 'easy';
const [error, json] = easyParse(jsonString, {initialValue: {}});

console.log(json.xxx); // If json is not exist, will return {} safely.

case: normal

import easyParse from 'easy-json-parse';
const jsonString = '{"easy": "easy"}';
const [error, json] = easyParse(jsonString);

console.log(json.easy); // easy

case: don't need error

import easyParse from 'easy-json-parse';
const jsonString = '{"easy": "easy"}';
const [, json] = easyParse(jsonString);

console.log(json.easy); // easy

Syntax

Parameters

text

the string to parse as JSON

options

options.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

options.initialValue(optional)

  • init value for json.

Returns

Array [error, value]

License

MIT

Copyright (c) 2019 蓝色的秋风

Keywords

FAQs

Last updated on 29 Jul 2019

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc