# no-unsafe-json-serialization

Rule `unicorn/no-unsafe-json-serialization`. Disallow known values that JSON serialization cannot represent faithfully.

- Source: unicorn
- Technologies: JavaScript
- Kind: correctness
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 9.9M weekly downloads of presets that enable it
- Trend: +28.3% last quarter
- Rank: #1054 by reach, #1309 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/no-unsafe-json-serialization` | no | suggestion | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/no-unsafe-json-serialization.md) |

## Code examples

### Incorrect: the rule reports this

```js
JSON.stringify({permissions: new Set(['read'])});
JSON.stringify(new Map([['name', 'unicorn']]));
JSON.stringify({count: 1n});
JSON.stringify(/unicorn/gi);
Response.json({permissions: new Set(['read'])});
```

### Correct: the rule accepts this

```js
JSON.stringify({permissions: Array.from(new Set(['read']))});
JSON.stringify(Object.fromEntries(new Map([['name', 'unicorn']])));
JSON.stringify({count: String(1n)});
JSON.stringify({optional: undefined});
Response.json({permissions: Array.from(new Set(['read']))});

const pattern = /unicorn/gi;
JSON.stringify({source: pattern.source, flags: pattern.flags});
```

```js
JSON.stringify({permissions: new Set(['read'])}, (key, value) => {
  return value instanceof Set ? Array.from(value) : value;
});

JSON.stringify({permissions: new Set(['read']), name: 'unicorn'}, ['name']);

JSON.stringify({
  permissions: new Set(['read']),
  toJSON() {
    return {permissions: Array.from(this.permissions)};
  },
});
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/no-unsafe-json-serialization": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/no-unnecessary-parameters](https://lintradar.dev/rules/unicorn/no-unnecessary-parameters.md): Disallow parameters that receive the same value at every call.
- [unicorn/no-unsafe-property-key](https://lintradar.dev/rules/unicorn/no-unsafe-property-key.md): Disallow unsafe values as property keys.
- [unicorn/no-unnecessary-boolean-comparison](https://lintradar.dev/rules/unicorn/no-unnecessary-boolean-comparison.md): Disallow unnecessary comparisons against boolean literals.
- [unicorn/no-useless-else](https://lintradar.dev/rules/unicorn/no-useless-else.md): Disallow `else` after a statement that exits.
- [unicorn/no-unnecessary-array-flat-map](https://lintradar.dev/rules/unicorn/no-unnecessary-array-flat-map.md): Disallow unnecessary use of `Array#flatMap()`.
- [unicorn/operator-assignment](https://lintradar.dev/rules/unicorn/operator-assignment.md): Require assignment operator shorthand where possible.

---

Web page: https://lintradar.dev/rules/unicorn/no-unsafe-json-serialization. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
