Socket
Socket
Sign inDemoInstall

@ambassify/json-parse-safe

Package Overview
Dependencies
Maintainers
3
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ambassify/json-parse-safe

Like JSON.parse, but doesn't throw on invalid JSON.


Version published
Weekly downloads
16
Maintainers
3
Weekly downloads
 
Created
Source

json-parse-safe

Like JSON.parse, but doesn't throw on invalid JSON.

Usage

Takes the same arguments as JSON.parse.

const parse = require('json-parse-safe');
const validJson = '{ "foo": "bar" }';
const invalidJson = 'foo';

// Simple usage
console.log(parse(validJson)); // { foo: 'bar' }
console.log(parse(invalidJson)); // null

// Pass a reviver just like the real JSON.parse
parse('{"p": 5}', (key, value) =>
  typeof value === 'number'
    ? value * 2 // return value * 2 for numbers
    : value     // return everything else unchanged
);
// { p: 10 }

// choose a defaultValue for invalid json
console.log(parse(invalidJson, undefined, 'WHOOPS')); // 'WHOOPS'

Keywords

FAQs

Package last updated on 25 Sep 2018

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