Skip to main content

JSONRepair: Your Go-To Tool For Fixing Broken JSON

JSON (JavaScript Object Notation) is a ubiquitous data format used across a myriad of applications and systems. However, creating perfectly valid JSON can be challenging, and often leads to errors, inconsistencies, and data corruption. This is where JSONRepair comes to the rescue.


JSONRepair is a powerful Node.js library designed to fix common issues found in broken JSON documents. It can detect and repair a wide range of problems, making it an invaluable tool for developers, data analysts, and anyone working with JSON data.


What JSONRepair Can Fix:

JSONRepair tackles a comprehensive set of common JSON issues, including:

  • Missing Quotes: Adds missing quotes around keys.
  • Missing Escape Characters: Inserts necessary escape characters for special characters.
  • Missing Commas: Inserts missing commas between elements in arrays and objects.
  • Missing Closing Brackets: Adds missing closing brackets for arrays and objects.
  • Truncated JSON: Repairs truncated JSON by adding missing elements or closing brackets.
  • Single Quotes to Double Quotes: Replaces single quotes with double quotes.
  • Special Quote Characters: Replaces special quote characters like “...” with regular double quotes.
  • Special White Space Characters: Replaces special white space characters with regular spaces.
  • Python Constants: Converts Python constants like None, True, and False to null, true, and false.
  • Trailing Commas: Removes trailing commas.
  • Comments: Strips comments like /* ... */ and // ...
  • Ellipsis: Strips ellipsis in arrays and objects like [1, 2, 3, ...].
  • JSONP Notation: Strips JSONP notation like callback({ ... }).
  • Escaped Strings: Removes unnecessary escape characters from escaped strings.
  • MongoDB Data Types: Converts MongoDB data types like NumberLong(2) and ISODate("2012-12-19T06:01:17.171Z") to their JSON equivalents.
  • String Concatenation: Concatenates strings like "long text" + "more text on next line".
  • Newline Delimited JSON: Turns newline delimited JSON into a valid JSON array.


Using JSONRepair:

JSONRepair offers multiple ways to use it, catering to diverse needs and environments:


ES Modules: 

Import the jsonrepair function directly using ES modules:


import { jsonrepair } from 'jsonrepair';


try {

  const json = "{name: 'John'}";

  const repaired = jsonrepair(json);

  console.log(repaired); // '{"name": "John"}'

} catch (err) {

  console.error(err);

}


Streaming API (Node.js): 

Use the jsonrepairTransform function within a Node.js stream pipeline:


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');

  }

});


CommonJS: 

Use JSONRepair within a CommonJS environment:


const { jsonrepair } = require('jsonrepair');

const json = "{name: 'John'}";

console.log(jsonrepair(json)); // '{"name": "John"}'


UMD (Browser): 

Use JSONRepair within a web browser using UMD:


<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 JSONRepair in Python via PythonMonkey:


import pythonmonkey


jsonrepair = pythonmonkey.require('jsonrepair').jsonrepair


json = "[1,2,3,"

repaired = jsonrepair(json)

print(repaired) 

# [1,2,3]


CLI for Effortless Repair:

JSONRepair also provides a powerful command-line interface (CLI) for convenient repair operations:


# Install JSONRepair globally

$ npm install -g jsonrepair


# Repair a file, output to console

$ jsonrepair broken.json 


# Repair a file, output to file

$ jsonrepair broken.json > repaired.json 


# Repair a file, output to file (using the --output option)

$ jsonrepair broken.json --output repaired.json


# Repair a file, replace the file itself

$ jsonrepair broken.json --overwrite


# Repair data from an input stream

$ cat broken.json | jsonrepair 


# Repair data from an input stream, output to file

$ cat broken.json | jsonrepair > repaired.json 


Conclusion:

JSONRepair is a robust and versatile tool that simplifies the task of working with potentially broken JSON. Its extensive repair capabilities, diverse usage options, and efficient streaming API make it an indispensable asset for any project involving JSON data. Whether you're a developer, data analyst, or simply need to clean up a messy JSON file, JSONRepair has you covered.

https://www.npmjs.com/package/jsonrepair

Comments

Topics

Show more