{"id":15290157,"url":"https://github.com/npm/json-parse-even-better-errors","last_synced_at":"2025-10-07T03:31:21.083Z","repository":{"id":57143870,"uuid":"211384370","full_name":"npm/json-parse-even-better-errors","owner":"npm","description":"get better errors","archived":false,"fork":true,"pushed_at":"2024-09-04T21:50:26.000Z","size":195,"stargazers_count":20,"open_issues_count":1,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-09-04T22:10:05.319Z","etag":null,"topics":["npm-cli"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"zkat/json-parse-better-errors","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/npm.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":"SECURITY.md","support":null}},"created_at":"2019-09-27T19:01:33.000Z","updated_at":"2024-09-04T21:49:25.000Z","dependencies_parsed_at":"2023-01-26T11:30:44.467Z","dependency_job_id":null,"html_url":"https://github.com/npm/json-parse-even-better-errors","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/npm%2Fjson-parse-even-better-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/npm%2Fjson-parse-even-better-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/npm%2Fjson-parse-even-better-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/npm%2Fjson-parse-even-better-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/npm","download_url":"https://codeload.github.com/npm/json-parse-even-better-errors/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219004269,"owners_count":16422833,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["npm-cli"],"created_at":"2024-09-30T16:05:48.509Z","updated_at":"2025-10-07T03:31:16.049Z","avatar_url":"https://github.com/npm.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json-parse-even-better-errors\n\n[`json-parse-even-better-errors`](https://github.com/npm/json-parse-even-better-errors)\nis a Node.js library for getting nicer errors out of `JSON.parse()`,\nincluding context and position of the parse errors.\n\nIt also preserves the newline and indentation styles of the JSON data, by\nputting them in the object or array in the `Symbol.for('indent')` and\n`Symbol.for('newline')` properties.\n\n## Install\n\n`$ npm install --save json-parse-even-better-errors`\n\n## Table of Contents\n\n* [Example](#example)\n* [Features](#features)\n* [Contributing](#contributing)\n* [API](#api)\n  * [`parse`](#parse)\n\n### Example\n\n```javascript\nconst parseJson = require('json-parse-even-better-errors')\n\nparseJson('\"foo\"') // returns the string 'foo'\nparseJson('garbage') // more useful error message\nparseJson.noExceptions('garbage') // returns undefined\n```\n\n### Features\n\n* Like JSON.parse, but the errors are better.\n* Strips a leading byte-order-mark that you sometimes get reading files.\n* Has a `noExceptions` method that returns undefined rather than throwing.\n* Attaches the newline character(s) used to the `Symbol.for('newline')`\n  property on objects and arrays.\n* Attaches the indentation character(s) used to the `Symbol.for('indent')`\n  property on objects and arrays.\n\n## Indentation\n\nTo preserve indentation when the file is saved back to disk, use\n`data[Symbol.for('indent')]` as the third argument to `JSON.stringify`, and\nif you want to preserve windows `\\r\\n` newlines, replace the `\\n` chars in\nthe string with `data[Symbol.for('newline')]`.\n\nFor example:\n\n```js\nconst txt = await readFile('./package.json', 'utf8')\nconst data = parseJsonEvenBetterErrors(txt)\nconst indent = Symbol.for('indent')\nconst newline = Symbol.for('newline')\n// .. do some stuff to the data ..\nconst string = JSON.stringify(data, null, data[indent]) + '\\n'\nconst eolFixed = data[newline] === '\\n' ? string\n  : string.replace(/\\n/g, data[newline])\nawait writeFile('./package.json', eolFixed)\n```\n\nIndentation is determined by looking at the whitespace between the initial\n`{` and `[` and the character that follows it.  If you have lots of weird\ninconsistent indentation, then it won't track that or give you any way to\npreserve it.  Whether this is a bug or a feature is debatable ;)\n\n### API\n\n#### \u003ca name=\"parse\"\u003e\u003c/a\u003e `parse(txt, reviver = null, context = 20)`\n\nWorks just like `JSON.parse`, but will include a bit more information when\nan error happens, and attaches a `Symbol.for('indent')` and\n`Symbol.for('newline')` on objects and arrays.  This throws a\n`JSONParseError`.\n\n#### \u003ca name=\"parse\"\u003e\u003c/a\u003e `parse.noExceptions(txt, reviver = null)`\n\nWorks just like `JSON.parse`, but will return `undefined` rather than\nthrowing an error.\n\n#### \u003ca name=\"jsonparseerror\"\u003e\u003c/a\u003e `class JSONParseError(er, text, context = 20, caller = null)`\n\nExtends the JavaScript `SyntaxError` class to parse the message and provide\nbetter metadata.\n\nPass in the error thrown by the built-in `JSON.parse`, and the text being\nparsed, and it'll parse out the bits needed to be helpful.\n\n`context` defaults to 20.\n\nSet a `caller` function to trim internal implementation details out of the\nstack trace.  When calling `parseJson`, this is set to the `parseJson`\nfunction.  If not set, then the constructor defaults to itself, so the\nstack trace will point to the spot where you call `new JSONParseError`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnpm%2Fjson-parse-even-better-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnpm%2Fjson-parse-even-better-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnpm%2Fjson-parse-even-better-errors/lists"}