# parse-json

> Parse JSON with more helpful errors

Latest version **8.3.0** (published 2025-04-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install parse-json
pnpm add parse-json
yarn add parse-json
bun add parse-json
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 8.3.0 |
| Published | 2025-04-09 |
| First published | 2015-07-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 3 |
| Unpacked size | 10.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 371 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | parse, json, graceful, error, message, humanize, friendly, helpful, string |

## Links

- npm: https://www.npmjs.com/package/parse-json
- Repository: https://github.com/sindresorhus/parse-json
- Homepage: https://github.com/sindresorhus/parse-json#readme
- Issues: https://github.com/sindresorhus/parse-json/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/parse-json

## Dependencies (3)

- [type-fest](https://npm.io/package/type-fest.md) ^4.39.1
- [@babel/code-frame](https://npm.io/package/@babel/code-frame.md) ^7.26.2
- [index-to-position](https://npm.io/package/index-to-position.md) ^1.1.0

## 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

- 8.3.0 (latest) — 2025-04-09
- 8.2.0 — 2025-03-21
- 8.1.0 — 2023-11-22
- 8.0.1 — 2023-11-09
- 8.0.0 — 2023-11-02
- 7.1.1 — 2023-10-27
- 7.1.0 — 2023-09-01
- 7.0.0 — 2023-04-07
- 6.0.2 — 2021-11-21
- 6.0.1 — 2021-11-15
- 6.0.0 — 2021-11-04
- 5.2.0 — 2021-01-18
- 5.1.0 — 2020-08-22
- 5.0.1 — 2020-07-24
- 5.0.0 — 2019-07-02
- … 7 more at https://npm.io/package/parse-json/versions

## README

# parse-json

> Parse JSON with more helpful errors

## Install

```sh
npm install parse-json
```

## Usage

```js
import parseJson, {JSONError} from 'parse-json';

const json = '{\n\t"foo": true,\n}';


JSON.parse(json);
/*
SyntaxError: Expected double-quoted property name in JSON at position 16 (line 3 column 1)
*/


parseJson(json);
/*
JSONError: Expected double-quoted property name in JSON at position 16 (line 3 column 1)

  1 | {
  2 |   "foo": true,
> 3 | }
    | ^
*/


parseJson(json, 'foo.json');
/*
JSONError: Expected double-quoted property name in JSON at position 16 (line 3 column 1) in foo.json

  1 | {
  2 |   "foo": true,
> 3 | }
    | ^
  fileName: 'foo.json',
  [cause]: SyntaxError: Expected double-quoted property name in JSON at position 16 (line 3 column 1)
      at JSON.parse (<anonymous>)
      at ...
*/


// You can also add the filename at a later point
try {
	parseJson(json);
} catch (error) {
	if (error instanceof JSONError) {
		error.fileName = 'foo.json';
	}

	throw error;
}
/*
JSONError: Expected double-quoted property name in JSON at position 16 (line 3 column 1) in foo.json

  1 | {
  2 |   "foo": true,
> 3 | }
    | ^

  fileName: 'foo.json',
  [cause]: SyntaxError: Expected double-quoted property name in JSON at position 16 (line 3 column 1)
      at JSON.parse (<anonymous>)
      at ...
*/
```

## API

### parseJson(string, reviver?, filename?)

Throws a `JSONError` when there is a parsing error.

#### string

Type: `string`

#### reviver

Type: `Function`

Prescribes how the value originally produced by parsing is transformed, before being returned. See [`JSON.parse` docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter
) for more.

#### filename

Type: `string`

The filename displayed in the error message.

### JSONError

Exposed for `instanceof` checking.

#### fileName

Type: `string`

The filename displayed in the error message.

#### codeFrame

Type: `string`

The printable section of the JSON which produces the error.

#### rawCodeFrame

Type: `string`

The raw version of `codeFrame` without colors.

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