Socket
Socket
Sign inDemoInstall

parse-json

Package Overview
Dependencies
2
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    parse-json

Parse JSON with more helpful errors


Version published
Maintainers
1
Install size
48.6 kB
Created

Package description

What is parse-json?

The parse-json npm package is used for parsing JSON strings. It provides an API for parsing JSON with more helpful error messages than the native JSON.parse. It can also handle trailing commas and comments in JSON strings, which are not supported by the standard JSON.parse.

What are parse-json's main functionalities?

Parse JSON with better error messages

This feature allows users to parse JSON strings and get more informative error messages when the JSON is invalid. It helps in debugging and fixing the JSON content.

const parseJson = require('parse-json');

try {
  const obj = parseJson('{"foo": true,}');
} catch (error) {
  console.error(error.message);
}

Parse JSON with comments

parse-json can handle JSON strings that contain comments, which are not typically allowed in JSON. This is useful when dealing with configurations or other JSON files where comments might be present.

const parseJson = require('parse-json');

const jsonWithComments = '{/* comment */ "foo": true}'
const obj = parseJson(jsonWithComments);

Parse JSON with trailing commas

This feature allows the parsing of JSON strings that have trailing commas after the last element in an object or array, which is not permitted in standard JSON.

const parseJson = require('parse-json');

const jsonWithTrailingComma = '{"foo": true,}'
const obj = parseJson(jsonWithTrailingComma);

Other packages similar to parse-json

Readme

Source

parse-json Build Status

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: Trailing comma in object at 3:1
}
^
*/


parseJson(json, 'foo.json');
/*
JSONError: Trailing comma in object in foo.json:3:1
}
^
*/


// You can also add the filename at a later point
try {
	parseJson(json);
} catch (err) {
	err.fileName = 'foo.json';
	throw err;
}
/*
JSONError: Trailing comma in object in foo.json:3:1
}
^
*/

API

parseJson(input, [reviver], [filename])

input

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.

License

MIT © Sindre Sorhus

Keywords

FAQs

Last updated on 17 Aug 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc