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 using npm or yarn:
npm i destr
# or
yarn add destr
Import into your Node.js project:
// CommonJS
const destr = require('destr')
// ESM
import destr from 'destr'
import destr from 'https://deno.land/x/destr/src/index.ts'
console.log(destr('{ "deno": "yay" }'))
Please note that destr
is little bit slower when parsing a standard JSON string mainly because of transform to avoid prototype pollution which can lead to serious security issues if not being sanetized. In the other words, destr
is better when input is not always a json string or from untrsuted source like request body.
Fast fallback to input if is not string:
// Uncaught SyntaxError: Unexpected token u in JSON at position 0
JSON.parse()
// undefined
destr()
// JSON.parse x 5,324,474 ops/sec ±0.65% (94 runs sampled)
JSON.parse(3.14159265359)
// destr x 657,187,095 ops/sec ±0.06% (98 runs sampled)
destr(3.14159265359)
Fast lookup for known string values:
// Uncaught SyntaxError: Unexpected token T in JSON at position 0
JSON.parse('TRUE')
// true
destr('TRUE')
// JSON.parse x 10,407,488 ops/sec ±0.30% (97 runs sampled)
JSON.parse('true')
// destr x 88,634,032 ops/sec ±0.32% (95 runs sampled)
destr('true')
Fallback to original value if parse fails (empty or any plain string):
// Uncaught SyntaxError: Unexpected token s in JSON at position 0
// JSON.parse (try-catch) x 248,212 ops/sec ±1.22% (84 runs sampled
JSON.parse('salam')
// destr x 30,867,179 ops/sec ±0.49% (94 runs sampled)
destr('salam')
Avoid prototype pollution:
const input = '{ "user": { "__proto__": { "isAdmin": true } } }'
// { user: { __proto__: { isAdmin: true } } }
JSON.parse(input)
// { user: {} }
destr(input)
MIT. Made with 💖
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.