jsonrepair

Back to blog

How to fix broken JSON without stress (JS and Python)

β€’
4 min read
Table of Contents

Everyone developing web applications, especially backend developers, knows an error like this all too well:

SyntaxError: JSON.parse: unexpected character at line 1 column 26 of the JSON data

jsonrepair is a small but powerful tool that attempts to fix damaged or invalid JSON so it can be correctly parsed again. Here are links to the documentation, the author’s blog post, the GitHub repository, and an online playground to try it out.

Description and features of jsonrepair

The tool analyzes an invalid JSON string and tries to transform it into a valid format. It fixes issues such as:

  • missing quotes around keys
  • trailing or missing commas
  • incorrect or missing brackets
  • invalid quotes – for example, β€˜text’ β†’ "text"
  • replacing JS constants undefined, NaN, Infinity with valid JSON values
  • replacing Python constants None, True, False with valid JSON values
  • comments and other non-JSON syntax (e.g., from JS objects)
  • removing escape characters from strings or fixing incorrect word-breaks
  • converting single-line JSON with newline characters into a valid array of objects

How to get started

Installation via npm

npm install jsonrepair

Usage in Node.js

import { jsonrepair } from 'jsonrepair';

const brokenJson = `
{
  name: 'Pizzeria Roma',
  address: "Main Street 123",
}`;

const fixed = jsonrepair(brokenJson);
// '{
//   "name": "Pizzeria Roma",
//   "address": "Main Street 123",
//  }'

Usage in CLI

jsonrepair broken.json > fixed.json

Usage in Python using PythonMonkey

It is possible to use jsonrepair directly in Python via the PythonMonkey package, which allows you to run JavaScript code within Python.

npm install jsonrepair
pip install pythonmonkey
# repair.py

import pythonmonkey

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

json = "[1,2,3,"
repaired = jsonrepair(json)
print(repaired)
# Output: [1,2,3]

Examples of fixes

Classic problems

{
  name: "Test", // comment
  id: NaN,
}
{
  "name": "Test",
  "id": null
}

JSONP wrapper

callback({
  "id": 1,
  "name": "Tony"
});
{
  "id": 1,
  "name": "Tony"
}

Multiple JSON objects separated by new lines

{ "id": 1, "name": "Anna" }
{ "id": 2, "name": "Henry" }
[
  { "id": 1, "name": "Anna" },
  { "id": 2, "name": "Henry" }
]

MongoDB format

{
  "_id": ObjectId("5f43a1b2e4d1c4a3f0a7f123"),
  "created": ISODate("2022-10-12T12:00:00Z"),
  "value": NumberLong(100)
}
{
  "_id": "5f43a1b2e4d1c4a3f0a7f123",
  "created": "2022-10-12T12:00:00Z",
  "value": 100
}

Advantages

  • Fixes common errors without manual debugging
  • Great for debugging or automatic repair of input data
  • Can be integrated into tests, CI tools, forms, etc.

Limitations and disadvantages

  • It won’t fix everything – if the input is extremely broken, it may fail
  • Automatic repair may not always match the original intent – while jsonrepair creates valid JSON, the result might differ from what was originally intended
  • Should be used primarily for debugging or visualization

Examples of jsonrepair integration into larger software

  • When debugging responses from an API that returns malformed JSON
  • When parsing user inputs
  • In developer tools or editors
  • In CI pipelines – automatic repair of JSON logs before processing

Conclusion

jsonrepair is a useful tool for the automatic repair of syntactically invalid JSON formats. It allows for the simple transformation of erroneous or incomplete JSON inputs into valid structures that can be further processed.

An alternative package is, for example, https://github.com/RyanMarcus/dirty-json