Install destr
mainYou can install destr using your preferred package manager for Node.js environments.
# npm
npm i destr
# yarn
yarn add destr
# pnpm
pnpm i destrrepository·main·Indexed 23 days ago
https://github.com/unjs/destrA faster, secure, and convenient alternative to JSON.parse. destr handles untrusted input by preventing prototype pollution and providing graceful fallbacks instead of throwing errors. It includes a standard destr function for resilient parsing and a safeDestr function for strict parsing that throws errors on invalid JSON.
You can install destr using your preferred package manager for Node.js environments.
# npm
npm i destr
# yarn
yarn add destr
# pnpm
pnpm i destrImport destr and safeDestr using ESM or CommonJS depending on your project setup. For Deno, import directly from the Deno land URL.
// Node.js ESM
import { destr, safeDestr } from "destr";
// Node.js CommonJS
const { destr, safeDestr } = require("destr");
// Deno
import { destr, safeDestr } from "https://deno.land/x/destr/src/index.ts";The destr function is a safer and more convenient alternative to JSON.parse. It provides several key benefits:
unknown. You can provide a generic type to ensure the output is well-typed."TRUE" to true.safeDestr. Unlike destr, safeDestr will throw an error if the input is not a valid JSON string or if parsing fails (though non-string values and built-ins are still returned as-is).The destr() function is used to parse a value (typically a string) into its corresponding JavaScript type. It is designed to be fast and secure against prototype pollution.
Key behaviors:
true, false, null, undefined, NaN, and infinity.options.strict is not enabled, it falls back to returning the original input value.__proto__ and constructor keys to prevent prototype pollution, issuing a warning to the console.The safeDestr() function is a wrapper around destr() that enforces strict: true.
Unlike the standard destr(), which falls back to the original input if parsing fails, safeDestr() will throw an error if the input is not valid JSON or if potential prototype pollution is detected. Use this when you require guaranteed valid JSON output and want to handle parsing errors explicitly.
destr in a CommonJS environment, you can require the module directly. The module exports the destr function as the default export, but it also attaches destr and safeDestr as named properties to that function to allow for mixed default and named imports.The destr() and safeDestr() functions accept an optional options object of type Options to control strictness.
| Option | Type | Default | Description |
|---|---|---|---|
strict | boolean | false | When true, destr() will throw a SyntaxError for invalid JSON or an Error for suspected prototype pollution instead of falling back to the original value. |
export type Options = {
strict?: boolean;
};The library provides two primary functions for safe JSON parsing:
destr: Parses a string into a JavaScript object. If parsing fails, it returns the original input.safeDestr: Similar to destr, but specifically designed for safe parsing operations.