destr
Safe, fast alternative to JSON.parse — handles prototype pollution, JSON syntax errors, and common edge cases gracefully.
destr
General JSSafe, fast alternative to JSON.parse — handles prototype pollution, JSON syntax errors, and common edge cases gracefully.
Fit
Bundle (gzip)
721 B
1.4 kB raw
Install size includes transitive dependencies. Bundle size is gzipped browser payload.
Freshness
The Problem
JSON.parse throws on invalid input, making you wrap every call in try/catch. It's also vulnerable to prototype pollution via __proto__ keys in malicious input, and doesn't handle non-string inputs gracefully.
What It Does
destr parses JSON safely — it returns the input as-is if it's not a string, returns undefined on parse errors instead of throwing, and strips __proto__, constructor, and prototype keys to prevent prototype pollution. It's a drop-in JSON.parse replacement for user-supplied or untrusted data.
Installation
npm install destrUsage Example
import { destr, safeDestr } from 'destr';
// Safe parsing — no try/catch needed
destr('{"name": "Alice"}'); // { name: 'Alice' }
destr('invalid json'); // 'invalid json' (returns as-is)
destr(undefined); // undefined
destr(42); // 42
// Prototype pollution prevention
destr('{"__proto__": {"admin": true}}'); // {} (strips __proto__)
// Strict mode — throws on invalid JSON
safeDestr('not json'); // throws SyntaxErrorRelated packages
Unified TypeScript SDK for building AI-powered apps — switch between 20+ LLM providers without rewriting code.
animejsGeneral JSLightweight JavaScript animation library with a tiny footprint and a powerful, expressive API.
AOSGeneral JSAn easy-to-use library that makes elements fade, slide, or zoom into view as the user scrolls down your page.
archiverGeneral JSStreaming interface for generating ZIP and TAR archives in Node.js — create compressed archives programmatically from files, directories, and buffers.
@formkit/auto-animateGeneral JS> Add smooth transitions to any element with a single line — zero-config animations for adds, removes, and moves.