8.1.0 • Published 5 months ago

parse-json v8.1.0

Weekly downloads
43,024,798
License
MIT
Repository
github
Last release
5 months ago

parse-json

Parse JSON with more helpful errors

Install

npm install parse-json

Usage

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

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


JSON.parse(json);
/*
undefined:3
}
^
SyntaxError: Unexpected token }
*/


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 | }
    | ^
*/

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

read-pkgcosmiconfigjest-configarchetype-libraryeasy-select-rnchinjowwchinjowvuedragdropuploadimagesreact-native-bluetooth2killi8n-react-native-fast-imagepipihome@kroonprins/pipectl-corern-send-sms@yannick243/rw-json-file@icanpm/api-master@arisageha/react-lazyload@arisageha/react-lazyload-fix@cashremit/cr-streamline-icons@almeidaa/ms@dubb/mickeyreact-native-template-rfbasenightmaretwitchcloud-archive-s3airscanairscan-examplebb-chatreact-native-esc-pos-sahaab@borisovart/atol-kkt-module@frxf/frxfdeneme323112url-xinightmarebot@fundefund/funde_ck@ntt_app/react-native-custom-notificationreact-native-custom-text-hwjamesreact-native-covid-sdkgql_din_modbitgetdainty-shared@massbit/indexer-managerafryxiconsreact-native-thanh-toast-librarymutasi-bca@jttechnic/interpreter@thanhnguyen14797/react-native-thanh-toast-library@eginnovations/eginnovations-capacitor-plugincthpb-plugin-social@unifiedts/engine@olivervorasai/slidermysql-formatpanqibaoreact-native-printer-brothersrn-pdf-reader-offlineawesome-fonts@newhorizon-tech/dd-npm-package-templatereact-native-shekhar-bridge-testcogoportutilsukor-remasteruncoded-connectwilscanner@oiti/documentoscopy-react-native@respondea/cordova-plugin-v-inappbrowser@mink-opn/build-tokensquoc-testreact-native-slider-kf@infinitebrahmanuniverse/nolb-parse-@prodam/prodam-types@saaspe/componentscli-scriptsplginexpand-react-bridgeopea-bootstraapluminos-ui-coresklif-ui-kitsklif-api@everything-registry/sub-chunk-2410iqra-calculatorjawwy-sdkjawwy_gamification_release@314oner_npm/universal-components-libraryreact-native-sphereuisphereuijawwy_libraryeginnovations-capacitor-pluginreact-native-credit-card-pkgp149-table@rabailriaz/hisaab-web-portalsklif-uireact-native-jawwy_samplezona-x@frodzislaw/front3web-extweb-ext-runweb-yii2web-component-tester-bundleweb-elements-iconsvue-size-trackerwc-starterkitwdio-cucumberwebche
8.1.0

5 months ago

8.0.1

6 months ago

8.0.0

6 months ago

7.1.1

6 months ago

7.1.0

8 months ago

7.0.0

1 year ago

6.0.1

2 years ago

6.0.2

2 years ago

6.0.0

2 years ago

5.2.0

3 years ago

5.1.0

4 years ago

5.0.1

4 years ago

5.0.0

5 years ago

4.0.0

6 years ago

3.0.0

7 years ago

2.2.0

9 years ago

2.1.0

9 years ago

2.0.0

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago