Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

jsonrepair

josdejong1.7mISC3.12.0TypeScript support: included

Repair broken JSON documents

simple, json, repair, fix, invalid, stream, streaming

readme

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
  • Repair truncated JSON
  • 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 ellipsis in arrays and objects like [1, 2, 3, ...]
  • 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" }

The jsonrepair library has streaming support and can handle infinitely large documents.

Install

$ npm install jsonrepair

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

Use

ES module

Use the jsonrepair function using an ES modules import:

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)
}

Streaming API

Use the streaming API in Node.js:

import { createReadStream, createWriteStream } from 'node:fs'
import { pipeline } from 'node:stream'
import { jsonrepairTransform } from 'jsonrepair/stream'

const inputStream = createReadStream('./data/broken.json')
const outputStream = createWriteStream('./data/repaired.json')

pipeline(inputStream, jsonrepairTransform(), outputStream, (err) => {
  if (err) {
    console.error(err)
  } else {
    console.log('done')
  }
})

// or using .pipe() instead of pipeline():
// inputStream
//   .pipe(jsonrepairTransform())
//   .pipe(outputStream)
//   .on('error', (err) => console.error(err))
//   .on('finish', () => console.log('done'))

CommonJS

Use in CommonJS (not recommended):

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

UMD

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>

Python

Use in Python via PythonMonkey.

  1. Install jsonrepair via npm install jsonrepair
  2. Install PythonMonkey via pip install pythonmonkey
  3. Use the libraries in a Python script:

     import pythonmonkey
    
     jsonrepair = pythonmonkey.require('jsonrepair').jsonrepair
    
     json = "[1,2,3,"
     repaired = jsonrepair(json)
     print(repaired) 
     # [1,2,3]

API

Regular API

You can use jsonrepair as a function or as a streaming transform. Broken JSON is passed to the function, and the function either returns the repaired JSON, or throws an JSONRepairError exception when an issue is encountered which could not be solved.

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

Streaming API

The streaming API is availabe in jsonrepair/stream and can be used in a Node.js stream. It consists of a transform function that can be used in a stream pipeline.

jsonrepairTransform(options?: { chunkSize?: number, bufferSize?: number }) : Transform

The option chunkSize determines the size of the chunks that the transform outputs, and is 65536 bytes by default. Changing chunkSize can influcence the performance.

The option bufferSize determines how many bytes of the input and output stream are kept in memory and is also 65536 bytes by default. This buffer is used as a "moving window" on the input and output. This is necessary because jsonrepair must look ahead or look back to see what to fix, and it must sometimes walk back the generated output to insert a missing comma for example. The bufferSize must be larger than the length of the largest string and whitespace in the JSON data, otherwise, and error is thrown when processing the data. Making bufferSize very large will result in more memory usage and less performance.

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 this message
--output,  -o       Output file
--overwrite         Overwrite the input file
--buffer            Buffer size in bytes, for example 64K (default) or 1M

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 --output 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

When implementing a fix or a new feature, it important to know that there are currently two implementations:

  • src/regular This is a non-streaming implementation. The code is small and works for files up to 512MB, ideal for usage in the browser.
  • src/streaming A streaming implementation that can be used in Node.js. The code is larger and more complex, and the implementation uses a configurable bufferSize and chunkSize. When the parsed document contains a string or number that is longer than the configured bufferSize, the library will throw an "Index out of range" error since it cannot hold the full string in the buffer. When configured with an infinite buffer size, the streaming implementation works the same as the regular implementation. In that case this out of range error cannot occur, but it makes the performance worse and the application can run out of memory when repairing large documents.

Both implementations are tested against the same suite of unit tests in src/index.test.ts.

Scripts:

Script Description
npm install Install the dependencies once
npm run build Build the library (ESM, CommonJs, and UMD output in the folder lib)
npm test Run the unit tests
npm run lint Run the linter (eslint)
npm run format Automatically fix linter issues
npm run build-and-test Run the linter, build all, and run unit tests and integration tests
npm run release Release a new version. This will lint, test, build, increment the version number, push the changes to git, add a git version tag, and publish the npm package.
npm run release-dry-run Run all release steps and see the change list without actually publishing:

License

Released under the ISC license.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

3.12.0 (2025-02-06)

Features

3.11.2 (2024-12-20)

Bug Fixes

  • handle repairing a missing comma at a newline (ecf9588)

3.11.1 (2024-12-03)

Bug Fixes

  • #134 broken link to browser bundle in package.json (4553795)

3.11.0 (2024-11-27)

Features

  • #133 repair the missing end quote of a string value containing commas in an object (1899f70)

3.10.0 (2024-11-05)

Features

3.9.0 (2024-10-21)

Features

3.8.1 (2024-09-18)

Bug Fixes

  • code style adjustments, use template literals consistently (594f535)

3.8.0 (2024-05-15)

Features

  • #125 strip leading commas in objects and arrays (0ee8597)
  • #127 skip ellipsis in arrays and objects (019e509)
  • strip ellipsis from empty objects and arrays (5731996)

3.7.1 (2024-05-09)

Bug Fixes

  • #126 more robust detection of MongoDB data types and JSONP callbacks (58fe64c)

3.7.0 (2024-04-24)

Features

  • turn invalid numbers into strings (b2c68f3)

Bug Fixes

3.6.1 (2024-04-11)

Bug Fixes

3.6.0 (2024-02-13)

Features

  • repair unescaped double quotes (WIP) (9e7b04b)

Bug Fixes

3.5.1 (2024-01-10)

Bug Fixes

  • #112 remove comments after a string containing a delimiter (6e12753)

3.5.0 (2023-12-07)

Features

  • #106 streaming support in Node.js (#111)

Bug Fixes

  • repair a string concat that is not followed by a string

3.4.1 (2023-11-12)

Bug Fixes

3.4.0 (2023-11-01)

Features

3.3.0 (2023-11-01)

Features

3.2.4 (2023-10-04)

Bug Fixes

  • #101 implement a smarter way to fix both missing end quotes and unescaped newline characters (51a4de9)

3.2.3 (2023-09-27)

Bug Fixes

  • #99 fix repairing single quoted strings containing quotes such as backtick (b4b9180)
  • repair numeric values with trailing zeros like 00789 by changing them into a string (399f593)

3.2.2 (2023-09-22)

Bug Fixes

  • #100 jsonrepair sometimes crashing when repairing missing quotes (regression since v3.2.1) (f573da2)

3.2.1 (2023-09-20)

Bug Fixes

  • #97 improved handling of missing start and end quotes (82df750)
  • #98 wrong position reported in the error message of invalid numbers (5093616)
  • throw an error on numbers with a leading zero instead of splitting them in two (829d3ee)

3.2.0 (2023-06-13)

Features

  • repair a missing object value (2cd756f)

Bug Fixes

  • #93 repair undefined values by replacing them with null (af348d7)

3.1.0 (2023-05-04)

Features

  • fix broken numbers at the end of the string (c42d9dd)
  • fix broken numbers at the end of the string (#91) (9ad00fd)

3.0.3 (2023-04-17)

Bug Fixes

  • #89 wrongly parsing strings that contain a double quote left or right (4023ece)

3.0.2 (2023-01-06)

Bug Fixes

  • error handling unicode characters containing a 9 (d665ec2)

3.0.1 (2022-12-20)

Bug Fixes

  • improve resolving unquoted strings and missing colon (45cd4e4)

2021-12-19, version 3.0.0

  • Complete rewrite of the parser in TypeScript, with improved performance.
  • Can repair some additional cases of broken JSON.

⚠ BREAKING CHANGES

  • Changed the API from default export import jsonrepair from 'jsonrepair' to named export import { jsonrepair} from 'jsonrepair'
  • Changed in UMD export from jsonrepair to JSONRepair.jsonrepair
  • Changed the error class to JSONRepairError with a property .position

2021-06-09, version 2.2.1

  • Improved handling of trailing commas.
  • Improved handling of newline delimited JSON containing commas.
  • Improved handling of repairing objects/arrays with missing closing bracket.

2021-04-01, version 2.2.0

  • Implement #16: turn an escaped string containing JSON into valid JSON.

2021-04-01, version 2.1.0

  • Implemented command line interface (CLI), see #34.

2021-03-01, version 2.0.1

  • Performance improvements.

2021-01-13, version 2.0.0

  • Renamed the library from simple-json-repair to jsonrepair. Thanks a lot @vmx for making this npm package name available!
  • Change source code from TypeScript to JavaScript. Reasons: TypeScript conflicts with using native ESM, requiring ugly workarounds. Due to some (old) TypeScript issues we also have to use @ts-ignore a lot. Using TypeScript makes running tests slower. And in this case, TypeScript hardly adds value since we have a very simple API and function signatures.

2020-11-06, version 1.1.0

  • Implement support for string concatenation.
  • Implement support for adding missing end brackets for objects and arrays.

2020-11-05, version 1.0.1

  • Fixed ESM and UMD builds missing in npm package.

2020-11-05, version 1.0.0

  • Initial release, code extracted from the library jsoneditor.