• Stars
    star
    209
  • Rank 181,777 (Top 4 %)
  • Language
    TypeScript
  • License
    Other
  • Created over 3 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Repair invalid JSON documents

jsonrepair

Repair invalid JSON documents.

Try it out in a minimal demo: https://josdejong.github.io/jsonrepair/

Use it in a full-fledged application: https://jsoneditoronline.org

Read the background article "How to fix JSON and validate it with ease"

The following issues can be fixed:

  • Add missing quotes around keys
  • Add missing escape characters
  • Add missing commas
  • Add missing closing brackets
  • Replace single quotes with double quotes
  • Replace special quote characters like โ€œ...โ€ with regular double quotes
  • Replace special white space characters with regular spaces
  • Replace Python constants None, True, and False with null, true, and false
  • Strip trailing commas
  • Strip comments like /* ... */ and // ...
  • Strip JSONP notation like callback({ ... })
  • Strip escape characters from an escaped string like {\"stringified\": \"content\"}
  • Strip MongoDB data types like NumberLong(2) and ISODate("2012-12-19T06:01:17.171Z")
  • Concatenate strings like "long text" + "more text on next line"
  • Turn newline delimited JSON into a valid JSON array, for example:
    { "id": 1, "name": "John" }
    { "id": 2, "name": "Sarah" }
    

Install

$ npm install jsonrepair

Note that in the lib folder, there are builds for ESM, UMD, and CommonJs.

Use

Use with an ES modules import (recommended):

import { jsonrepair } from 'jsonrepair'

try {
  // 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 = jsonrepair(json)
  
  console.log(repaired) // '{"name": "John"}'
} catch (err) {
  console.error(err)
}

Use in CommonJS (not recommended):

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

Use with UMD in the browser (not recommended):

<script src="/node_modules/jsonrepair/lib/umd/jsonrepair.js"></script>
<script>
  const { jsonrepair } = JSONRepair
  const json = "{name: 'John'}"
  console.log(jsonrepair(json)) // '{"name": "John"}'
</script>

API

// @throws JSONRepairError 
jsonrepair(json: string) : string

The function jsonrepair throws an exception JSONRepairError 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

Alternatives:

Similar libraries:

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 automatically fix linter issues:

$ npm run format

To run the linter, build all, and run unit tests and integration tests:

$ npm run build-and-test

Release

To release a new version:

$ npm run release

This will:

  • lint
  • test
  • build
  • increment the version number
  • push the changes to git, add a git version tag
  • publish the npm package

To try the build and see the change list without actually publishing:

$ npm run release-dry-run

License

Released under the ISC license.

More Repositories

1

mathjs

An extensive math library for JavaScript and Node.js
JavaScript
14,048
star
2

jsoneditor

A web-based tool to view, edit, format, and validate JSON
JavaScript
10,762
star
3

workerpool

Offload tasks to a pool of workers on node.js and in the browser
JavaScript
1,773
star
4

svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON
TypeScript
642
star
5

lossless-json

Parse JSON without risk of losing numeric information
TypeScript
364
star
6

ducktype

Flexible data validation using a ducktype interface. For JavaScript and Node.js.
JavaScript
90
star
7

csv42

A small and fast CSV parser with support for nested JSON
TypeScript
89
star
8

typed-function

Runtime type-checking for JavaScript functions
JavaScript
69
star
9

mathjs-expression-parser

Just the expression parser of mathjs
JavaScript
51
star
10

httputil

Easily perform HTTP requests in Java
Java
50
star
11

propagating-hammerjs

Extend hammer.js with event propagation
JavaScript
40
star
12

immutable-json-patch

Immutable JSON patch with support for reverting operations
TypeScript
29
star
13

mount-at-selector

Render React components like a Modal outside the main React app
JavaScript
15
star
14

customerservice

A small demonstration showing the power of CouchDB and AngularJS
JavaScript
12
star
15

table-xxl

Render tables with hundred thousands of rows in a webpage
JavaScript
7
star
16

jsondatastore

JSON document database running on Google Datastore
Java
6
star
17

distributed-app

A framework to run Node.js applications in a distributed way
JavaScript
6
star
18

rws

Reconnecting WebSocket for node.js and the browser
JavaScript
6
star
19

blog

Personal blog
HTML
4
star
20

annotationutil

Get aggregated annotations from a class, its interfaces, and its superclasses
Java
3
star
21

pocomath

A little proof-of-concept for organizing mathjs by module inclusion, avoiding factory functions.
JavaScript
3
star
22

react-hooks-for-classes

An experiment applying the concept of hooks to React class components
TypeScript
2
star
23

biggie

A big number library
TypeScript
2
star
24

svelte-jsoneditor-vite-dependency-issues

JavaScript
2
star
25

mandelbrot-explorer

A plain JavaScript Mandelbrot explorer
JavaScript
1
star
26

webpack_tree_shaking_issue

Minimum example of webpack 4 not being able to do treeshaking
JavaScript
1
star
27

react-native-mathjs-demo

A simple calculator built with react-native, powered by math.js
JavaScript
1
star
28

femtomath

JavaScript
1
star
29

timeblocks

timeblocks extension for vis.js
JavaScript
1
star
30

mathjs-codemirror

A mathjs editor in CodeMirror
TypeScript
1
star
31

csv-benchmark

JavaScript
1
star
32

speqmath-website

Website of speqmath.com
1
star
33

insight

A debug tooling experiment
JavaScript
1
star