parse-json

  • Version 8.1.0
  • Published
  • 9.52 kB
  • 3 dependencies
  • MIT license

Install

npm i parse-json
yarn add parse-json
pnpm add parse-json

Overview

Parse JSON with more helpful errors

Index

Functions

function parseJson

parseJson: {
(string: string, reviver?: Reviver, filename?: string): JsonObject;
(string: string, filename?: string): JsonObject;
};
  • Parse JSON with more helpful errors.

    Parameter string

    A valid JSON string.

    Parameter reviver

    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.

    Parameter filename

    The filename displayed in the error message.

    Returns

    A parsed JSON object.

    Throws

    A JSONError when there is a parsing error.

    Example 1

    import parseJson, {JSONError} from 'parse-json';
    const json = '{\n\t"foo": true,\n}';
    parseJson(json);
    // JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}'
    //
    // 1 | {
    // 2 | "foo": true,
    // > 3 | }
    // | ^
    parseJson(json, 'foo.json');
    // JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
    //
    // 1 | {
    // 2 | "foo": true,
    // > 3 | }
    // | ^
    // 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: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
    //
    // 1 | {
    // 2 | "foo": true,
    // > 3 | }
    // | ^
  • Parse JSON with more helpful errors.

    Parameter string

    A valid JSON string.

    Parameter filename

    The filename displayed in the error message.

    Returns

    A parsed JSON object.

    Throws

    A JSONError when there is a parsing error.

    Example 1

    import parseJson, {JSONError} from 'parse-json';
    const json = '{\n\t"foo": true,\n}';
    parseJson(json);
    // JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}'
    //
    // 1 | {
    // 2 | "foo": true,
    // > 3 | }
    // | ^
    parseJson(json, 'foo.json');
    // JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
    //
    // 1 | {
    // 2 | "foo": true,
    // > 3 | }
    // | ^
    // 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: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
    //
    // 1 | {
    // 2 | "foo": true,
    // > 3 | }
    // | ^

Classes

class JSONError

class JSONError extends Error {}
  • Exposed for instanceof checking.

property codeFrame

readonly codeFrame: string;
  • The printable section of the JSON which produces the error.

property fileName

fileName: string;
  • The filename displayed in the error message, if any.

property rawCodeFrame

readonly rawCodeFrame: string;
  • The raw version of codeFrame without colors.

Type Aliases

type Reviver

type Reviver = Parameters<(typeof JSON)['parse']>['1'];

    Package Files (1)

    Dependencies (3)

    Dev Dependencies (6)

    Peer Dependencies (0)

    No peer dependencies.

    Badge

    To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

    You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/parse-json.

    • Markdown
      [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/parse-json)
    • HTML
      <a href="https://www.jsdocs.io/package/parse-json"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>