# safe-json-decode

> A minimal package for encoding and decoding json without unhandled exceptions being thrown on failure

Latest version **1.1.0** (published 2025-07-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install safe-json-decode
pnpm add safe-json-decode
yarn add safe-json-decode
bun add safe-json-decode
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2025-07-14 |
| First published | 2022-08-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Vincent Sjögren |
| Maintainers | vinsjo |
| Keywords | ESM, CommonJS, JSON, stringify, parse, decode, encode |

## Links

- npm: https://www.npmjs.com/package/safe-json-decode
- Repository: https://github.com/vinsjo/safe-json-decode
- Homepage: https://github.com/vinsjo/safe-json-decode#readme
- Issues: https://github.com/vinsjo/safe-json-decode/issues
- npm.io page: https://npm.io/package/safe-json-decode

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.0 (latest) — 2025-07-14
- 1.0.6 — 2022-09-08
- 1.0.5 — 2022-09-08
- 1.0.4 — 2022-09-08
- 1.0.3 — 2022-09-08
- 1.0.2 — 2022-09-07
- 1.0.1 — 2022-08-19
- 1.0.0 — 2022-08-19

## README

# safe-json-decode

`safe-json-decode` is a minimal package for encoding and decoding json without unhandled exceptions being thrown on failure

Package is bundled using [unbuild](https://www.npmjs.com/package/unbuild)

## Installation

`npm i safe-json-decode`

### In Node.js

```js
// ESM:
import { safeJsonDecode, safeJsonEncode } from 'safe-json-decode';
// CommonJS:
const { safeJsonDecode, safeJsonEncode } = require('safe-json-decode');
```

## Usage

```js
// Same behavior as JSON.parse with valid JSON
safeJsonDecode('{ "foo": "bar" }'); // => {foo: 'bar'}
// Returns null if JSON.parse fails
safeJsonDecode('{ invalid }'); // => null


// Same behavior as JSON.stringify with value that can be serialized
safeJsonEncode({ foo: 'bar' }); // => '{"foo": "bar"}'
// Returns null if JSON.stringify fails
const objectWithCircularReference = {};
objectWithCircularReference.self = objectWithCircularReference;
safeJsonEncode(objectWithCircularReference);


// Handle side effects if error is caught

const handleError = (error) => console.error(error);

safeJsonDecode('{ invalid }', undefined, handleError); 
safeJsonEncode(objectWithCircularReference, undefined, undefined, handleError);
```

---
_Source: https://npm.io/package/safe-json-decode · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
