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.
simple-json-repair
Repair invalid JSON documents.
Try it out: https://josdejong.github.io/simple-json-repair/
The following issues can be fixed:
- Add missing quotes around keys
- Replace single quotes with double quotes
- Turn newline delimited JSON into a valid JSON array
- Add missing escape characters
- Replace special white space characters with regular spaces
- Replace special quote characters like
“...”
with regular double quotes - Concatenate strings like
"long text" + "more text on next line"
- Add missing commas
- Strip trailing commas
- Strip comments like
/* ... */
and // ...
- Strip JSONP notation like
callback({ ... })
- Strip MongoDB data types like
NumberLong(2)
and ISODate("2012-12-19T06:01:17.171Z")
- Replace Python constants
None
, True
, and False
with null
, true
, and false
Install
$ npm install simple-json-repair
Note that in the lib
folder, there are builds for ESM, UMD, and CommonJs.
Use
import simpleJsonRepair from 'simple-json-repair'
const json = '{name: \'John\'}'
const repaired = simpleJsonRepair(json)
console.log(repaired)
API
simpleJsonRepair(json: string) : string
The function simpleJsonRepair
throws an exception when an issue is encountered
which could not be solved. When no error is thrown, the output will be valid JSON.
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.