JSPM

simple-json-repair

1.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 275
  • Score
    100M100P100Q9134F
  • License ISC

Repair broken JSON documents

Package Exports

  • simple-json-repair

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (simple-json-repair) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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'

// The following is invalid JSON: is consists of JSON contents copied from 
// a JavaScript code base, where the keys are missing double quotes, 
// and strings are using single quotes:
const json = '{name: \'John\'}'

const repaired = simpleJsonRepair(json)
console.log(repaired) // '{"name": "John"}'

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.