# json-parse-even-better-errors

> JSON.parse with context information on error

Latest version **6.0.0** (published 2026-05-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-parse-even-better-errors
pnpm add json-parse-even-better-errors
yarn add json-parse-even-better-errors
bun add json-parse-even-better-errors
```

## Health

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

Positive: no vulnerabilities; has provenance; high maintenance score.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 6.0.0 |
| Published | 2026-05-08 |
| First published | 2019-09-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | ^22.22.2 \|\| ^24.15.0 \|\| >=26.0.0 |
| Dependencies | 0 |
| Unpacked size | 10 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 27 |
| Author | GitHub Inc. |
| Maintainers | saquibkhan, npm-cli-ops, reggi, owlstronaut |
| Keywords | JSON, parser |

## Links

- npm: https://www.npmjs.com/package/json-parse-even-better-errors
- Repository: https://github.com/npm/json-parse-even-better-errors
- Homepage: https://github.com/npm/json-parse-even-better-errors#readme
- Issues: https://github.com/npm/json-parse-even-better-errors/issues
- npm.io page: https://npm.io/package/json-parse-even-better-errors

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

- 6.0.0 (latest) — 2026-05-08
- 5.0.0 — 2025-10-22
- 4.0.0 — 2024-09-04
- 3.0.2 — 2024-05-04
- 3.0.1 — 2023-11-28
- 3.0.0 — 2022-10-10
- 2.3.1 — 2020-09-02
- 2.3.0 — 2020-08-20
- 2.2.0 — 2020-03-05
- 2.1.0 — 2020-03-05
- 2.0.1 — 2019-09-27
- 2.0.0 — 2019-09-27

## README

# json-parse-even-better-errors

[`json-parse-even-better-errors`](https://github.com/npm/json-parse-even-better-errors)
is a Node.js library for getting nicer errors out of `JSON.parse()`,
including context and position of the parse errors.

It also preserves the newline and indentation styles of the JSON data, by
putting them in the object or array in the `Symbol.for('indent')` and
`Symbol.for('newline')` properties.

## Install

`$ npm install --save json-parse-even-better-errors`

## Table of Contents

* [Example](#example)
* [Features](#features)
* [Contributing](#contributing)
* [API](#api)
  * [`parse`](#parse)

### Example

```javascript
const parseJson = require('json-parse-even-better-errors')

parseJson('"foo"') // returns the string 'foo'
parseJson('garbage') // more useful error message
parseJson.noExceptions('garbage') // returns undefined
```

### Features

* Like JSON.parse, but the errors are better.
* Strips a leading byte-order-mark that you sometimes get reading files.
* Has a `noExceptions` method that returns undefined rather than throwing.
* Attaches the newline character(s) used to the `Symbol.for('newline')`
  property on objects and arrays.
* Attaches the indentation character(s) used to the `Symbol.for('indent')`
  property on objects and arrays.

## Indentation

To preserve indentation when the file is saved back to disk, use
`data[Symbol.for('indent')]` as the third argument to `JSON.stringify`, and
if you want to preserve windows `\r\n` newlines, replace the `\n` chars in
the string with `data[Symbol.for('newline')]`.

For example:

```js
const txt = await readFile('./package.json', 'utf8')
const data = parseJsonEvenBetterErrors(txt)
const indent = Symbol.for('indent')
const newline = Symbol.for('newline')
// .. do some stuff to the data ..
const string = JSON.stringify(data, null, data[indent]) + '\n'
const eolFixed = data[newline] === '\n' ? string
  : string.replace(/\n/g, data[newline])
await writeFile('./package.json', eolFixed)
```

Indentation is determined by looking at the whitespace between the initial
`{` and `[` and the character that follows it.  If you have lots of weird
inconsistent indentation, then it won't track that or give you any way to
preserve it.  Whether this is a bug or a feature is debatable ;)

### API

#### <a name="parse"></a> `parse(txt, reviver = null, context = 20)`

Works just like `JSON.parse`, but will include a bit more information when
an error happens, and attaches a `Symbol.for('indent')` and
`Symbol.for('newline')` on objects and arrays.  This throws a
`JSONParseError`.

#### <a name="parse"></a> `parse.noExceptions(txt, reviver = null)`

Works just like `JSON.parse`, but will return `undefined` rather than
throwing an error.

#### <a name="jsonparseerror"></a> `class JSONParseError(er, text, context = 20, caller = null)`

Extends the JavaScript `SyntaxError` class to parse the message and provide
better metadata.

Pass in the error thrown by the built-in `JSON.parse`, and the text being
parsed, and it'll parse out the bits needed to be helpful.

`context` defaults to 20.

Set a `caller` function to trim internal implementation details out of the
stack trace.  When calling `parseJson`, this is set to the `parseJson`
function.  If not set, then the constructor defaults to itself, so the
stack trace will point to the spot where you call `new JSONParseError`.

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