What is jsonrepair?
The jsonrepair npm package is designed to fix common errors in JSON strings, making them valid JSON. It can handle issues such as missing quotes, trailing commas, and other common JSON syntax errors.
What are jsonrepair's main functionalities?
Repairing JSON strings
This feature allows you to repair a broken JSON string by fixing common syntax errors. In the example, the input JSON string has a missing quote around the key and a trailing comma, which are corrected by jsonrepair.
const jsonrepair = require('jsonrepair');
const brokenJson = '{key: "value",}';
const repairedJson = jsonrepair(brokenJson);
console.log(repairedJson); // Output: {"key": "value"}
Handling various JSON errors
This feature demonstrates jsonrepair's ability to handle multiple types of errors in a JSON string, such as trailing commas and missing quotes. The repaired JSON string is valid and can be parsed by JSON.parse.
const jsonrepair = require('jsonrepair');
const brokenJson = '{"key": "value", "key2": "value2",}';
const repairedJson = jsonrepair(brokenJson);
console.log(repairedJson); // Output: {"key": "value", "key2": "value2"}
Other packages similar to jsonrepair
jsonlint
jsonlint is a JSON parser and validator with a CLI. It can be used to validate and format JSON strings, but it does not repair broken JSON strings like jsonrepair. Instead, it provides error messages to help you manually fix the issues.
json5
json5 is a JSON parser that allows for more relaxed JSON syntax, such as allowing comments and trailing commas. While it can parse JSON with some errors, it does not specifically aim to repair broken JSON strings like jsonrepair.
jsonc-parser
jsonc-parser is a parser for JSON with comments (JSONC). It can parse JSON strings with comments and other relaxed syntax rules, but it does not focus on repairing broken JSON strings. It is more about extending JSON's capabilities.
jsonrepair
Repair invalid JSON documents.
Try it out: https://josdejong.github.io/jsonrepair/
The following issues can be fixed:
Install
$ npm install jsonrepair
Note that in the lib
folder, there are builds for ESM, UMD, and CommonJs.
Use
import jsonrepair from 'jsonrepair'
const json = '{name: \'John\'}'
const repaired = jsonrepair(json)
console.log(repaired)
API
jsonrepair(json: string) : string
The function jsonrepair
throws an exception when an issue is encountered
which could not be solved. When no error is thrown, the output will be valid JSON.
Command Line Interface (CLI)
When jsonrepair
is installed globally using npm, it can be used on the command line. To install jsonrepair
globally:
$ npm install -g jsonrepair
Usage:
$ jsonrepair [filename] {OPTIONS}
Options:
--version, -v Show application version
--help, -h Show help
Example usage:
$ jsonrepair broken.json # Repair a file, output to console
$ jsonrepair broken.json > repaired.json # Repair a file, output to file
$ jsonrepair broken.json --overwrite # Repair a file, replace the file itself
$ cat broken.json | jsonrepair # Repair data from an input stream
$ cat broken.json | jsonrepair > repaired.json # Repair data from an input stream, output to file
Develop
To build the library (ESM, CommonJs, and UMD output in the folder lib
):
$ npm install
$ npm run build
To run the unit tests:
$ npm test
To run the linter (eslint):
$ npm run lint
To run the linter, build all, and run unit tests and integration tests:
$ npm run build-and-test
License
Released under the ISC license.