Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
The `destr` npm package is designed to safely parse JSON strings without throwing an error for invalid JSON. It can return the original string if parsing fails, making it useful for handling dynamic JSON data that may not always be properly formatted. It also recognizes and correctly parses values like `null`, `true`, `false`, and `undefined`.
Safe JSON parsing
Safely parse a JSON string without throwing an error. If the string is not valid JSON, it returns the original string.
"const destr = require('destr');
const json = '{\"key\":\"value\"}';
const parsed = destr(json);
console.log(parsed); // Output: { key: 'value' }"
Parsing special JSON values
Correctly parse special JSON values such as `null`, `true`, `false`, and `undefined`, returning their corresponding JavaScript types.
"const destr = require('destr');
console.log(destr('null')); // Output: null
console.log(destr('true')); // Output: true
console.log(destr('false')); // Output: false
console.log(destr('undefined')); // Output: undefined"
Similar to `destr`, `json5` allows for parsing of JSON data with more lenient syntax rules, such as trailing commas and comments. However, `json5` focuses on extending JSON syntax to be more flexible, while `destr` focuses on safe parsing and handling special values.
This package offers functionality similar to `destr` by providing a safe way to parse JSON strings without throwing errors for invalid JSON. The main difference is in the API and specific handling of non-JSON values.
A faster, secure and convenient alternative for JSON.parse
.
Install dependency:
# npm
npm i destr
# yarn
yarn add destr
# pnpm
pnpm i destr
Import into your Node.js project:
// ESM
import { destr, safeDestr } from "destr";
// CommonJS
const { destr, safeDestr } = require("destr");
import { destr, safeDestr } from "https://deno.land/x/destr/src/index.ts";
console.log(destr('{ "deno": "yay" }'));
const obj = JSON.parse("{}"); // obj type is any
const obj = destr("{}"); // obj type is unknown by default
const obj = destr<MyInterface>("{}"); // obj is well-typed
🚀 Up to 500 faster than
JSON.parse
!
// Uncaught SyntaxError: Unexpected token u in JSON at position 0
JSON.parse();
// undefined
destr();
🚀 Up to 900 times faster than
JSON.parse
!
// Uncaught SyntaxError: Unexpected token T in JSON at position 0
JSON.parse("TRUE");
// true
destr("TRUE");
🚀 Up to 900 times faster than
JSON.parse
!
// Uncaught SyntaxError: Unexpected token s in JSON at position 0
JSON.parse("salam");
// "salam"
destr("salam");
Note: This fails in safe/strict mode with safeDestr
.
const input = '{ "user": { "__proto__": { "isAdmin": true } } }';
// { user: { __proto__: { isAdmin: true } } }
JSON.parse(input);
// { user: {} }
destr(input);
When using safeDestr
it will throw an error if the input is not a valid JSON string or parsing fails. (non string values and built-ins will be still returned as-is)
// Returns "[foo"
safeDestr("[foo");
// Throws an error
safeDestr("[foo", { strict: true });
Locally try with pnpm benchmark
. Below are esults on Node.js v18.16.0 with MBA M2.
Note destr
is sometimes little bit slower than JSON.parse
when parsing a valid JSON string mainly because of transform to avoid prototype pollution which can lead to serious security issues if not being sanitized. In the other words, destr
is better when input is not always a json string or from untrusted source like request body.
=== Non-string fallback ==
JSON.parse x 9,498,532 ops/sec ±0.57% (96 runs sampled)
destr x 153,323,211 ops/sec ±0.13% (99 runs sampled)
safeDestr x 64,237,062 ops/sec ±0.22% (96 runs sampled)
sjson:
@hapi/bourne x 9,190,459 ops/sec ±0.50% (93 runs sampled)
Fastest is destr
=== Known values ==
JSON.parse x 14,260,909 ops/sec ±0.54% (95 runs sampled)
destr x 72,916,945 ops/sec ±0.15% (98 runs sampled)
safeDestr x 36,544,906 ops/sec ±0.31% (98 runs sampled)
sjson x 11,157,730 ops/sec ±0.53% (96 runs sampled)
@hapi/bourne x 13,241,853 ops/sec ±0.73% (93 runs sampled)
Fastest is destr
=== plain string ==
JSON.parse (try-catch) x 10,603,912 ops/sec ±0.75% (91 runs sampled)
destr x 82,123,481 ops/sec ±2.37% (99 runs sampled)
safeDestr x 40,737,935 ops/sec ±0.97% (96 runs sampled)
sjson (try-catch) x 9,194,305 ops/sec ±1.96% (94 runs sampled)
@hapi/bourne x 10,816,232 ops/sec ±1.59% (90 runs sampled)
Fastest is destr
=== package.json ==
JSON.parse x 403,428 ops/sec ±0.31% (101 runs sampled)
destr x 338,668 ops/sec ±0.27% (97 runs sampled)
safeDestr x 335,756 ops/sec ±0.29% (98 runs sampled)
sjson x 355,493 ops/sec ±0.15% (101 runs sampled)
@hapi/bourne x 384,948 ops/sec ±0.24% (98 runs sampled)
Fastest is JSON.parse
=== broken object ==
JSON.parse (try-catch) x 406,262 ops/sec ±0.18% (100 runs sampled)
destr x 337,602 ops/sec ±0.37% (99 runs sampled)
safeDestr x 320,071 ops/sec ±0.35% (97 runs sampled)
sjson (try-catch) x 326,689 ops/sec ±0.41% (97 runs sampled)
@hapi/bourne x 313,024 ops/sec ±0.91% (94 runs sampled)
Fastest is JSON.parse (try-catch)
MIT. Made with 💖
v2.0.0
unkown
default (#68)safeDestr
(#70)destr
export (#69)changelogen
for release (75989d3)codecov.yml
(fa4d366)FAQs
A faster, secure and convenient alternative for JSON.parse
The npm package destr receives a total of 1,223,817 weekly downloads. As such, destr popularity was classified as popular.
We found that destr demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.