parse-json

WebJar for parse-json

License

License

MIT
Categories

Categories

JSON Data
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

parse-json
Last Version

Last Version

5.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

parse-json
WebJar for parse-json
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/sindresorhus/parse-json

Download parse-json

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/parse-json/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>parse-json</artifactId>
    <version>5.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/parse-json/
implementation 'org.webjars.npm:parse-json:5.2.0'
// https://jarcasting.com/artifacts/org.webjars.npm/parse-json/
implementation ("org.webjars.npm:parse-json:5.2.0")
'org.webjars.npm:parse-json:jar:5.2.0'
<dependency org="org.webjars.npm" name="parse-json" rev="5.2.0">
  <artifact name="parse-json" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='parse-json', version='5.2.0')
)
libraryDependencies += "org.webjars.npm" % "parse-json" % "5.2.0"
[org.webjars.npm/parse-json "5.2.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.webjars.npm : babel__code-frame jar [7.0.0,8)
org.webjars.npm : error-ex jar [1.3.1,2)
org.webjars.npm : json-parse-even-better-errors jar [2.3.0,3)
org.webjars.npm : lines-and-columns jar [1.1.6,2)

Project Modules

There are no modules declared in this project.

parse-json

Parse JSON with more helpful errors

Install

$ npm install parse-json

Usage

const parseJson = require('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 parseJson.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

Filename displayed in the error message.

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


Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

Versions

Version
5.2.0
5.1.0
5.0.1
5.0.0
4.0.0
3.0.0
2.2.0