Skip to content

Feature: Add JSON Repair operation with comprehensive malformed JSON fixing capabilities #2087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"json5": "^2.2.3",
"jsonata": "^2.0.3",
"jsonpath-plus": "^9.0.0",
"jsonrepair": "^3.13.0",
"jsonwebtoken": "8.5.1",
"jsqr": "^1.4.0",
"jsrsasign": "^11.1.0",
Expand Down
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@
"JavaScript Minify",
"JSON Beautify",
"JSON Minify",
"JSON Repair",
"XML Beautify",
"XML Minify",
"SQL Beautify",
Expand Down
48 changes: 48 additions & 0 deletions src/core/operations/JSONRepair.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @author maojunxyz [maojun@linux.com]
* @copyright Crown Copyright 2025
* @license Apache-2.0
*/

import OperationError from "../errors/OperationError.mjs";
import Operation from "../Operation.mjs";

/**
* JSON Repair operation
*/
class JSONRepair extends Operation {

/**
* JSONRepair constructor
*/
constructor() {
super();

this.name = "JSON Repair";
this.module = "Code";
this.description = "Attempts to repair invalid JSON by fixing common issues such as missing quotes around keys, trailing commas, single quotes, missing brackets, and more.<br><br>This operation can fix:<br>• Missing quotes around keys<br>• Single quotes instead of double quotes<br>• Trailing commas<br>• Missing commas<br>• Missing closing brackets<br>• Python constants (None, True, False)<br>• Comments<br>• JSONP notation<br>• And many other common JSON formatting issues<br><br>Uses the jsonrepair library for comprehensive JSON repair.<br><br>Tags: json fix, json repair, json validate, json format";
this.inputType = "string";
this.outputType = "string";
this.args = [];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
async run(input, args) {
if (!input) return "";

try {
// Dynamic import of jsonrepair to handle potential module loading issues
const { jsonrepair } = await import("jsonrepair");
return jsonrepair(input);

} catch (err) {
throw new OperationError("Unable to repair JSON. The input contains errors that cannot be automatically fixed.\n" + err.message);
}
}
}

export default JSONRepair;
1 change: 1 addition & 0 deletions tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import "./tests/JA3SFingerprint.mjs";
import "./tests/Jsonata.mjs";
import "./tests/JSONBeautify.mjs";
import "./tests/JSONMinify.mjs";
import "./tests/JSONRepair.mjs";
import "./tests/JSONtoCSV.mjs";
import "./tests/Jump.mjs";
import "./tests/JWK.mjs";
Expand Down
175 changes: 175 additions & 0 deletions tests/operations/tests/JSONRepair.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/**
* JSONRepair tests.
*
* @author maojunxyz [maojun@linux.com]
*
* @copyright Crown Copyright 2025
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";

TestRegister.addTests([
{
name: "JSON Repair: empty string",
input: "",
expectedOutput: "",
recipeConfig: [
{
op: "JSON Repair",
args: [],
},
],
},
{
name: "JSON Repair: valid JSON unchanged",
input: "{\"name\": \"John\", \"age\": 30}",
expectedOutput: "{\"name\": \"John\", \"age\": 30}",
recipeConfig: [
{
op: "JSON Repair",
args: [],
},
],
},
{
name: "JSON Repair: missing quotes around keys",
input: "{name: \"John\", age: 30}",
expectedOutput: "{\"name\": \"John\", \"age\": 30}",
recipeConfig: [
{
op: "JSON Repair",
args: [],
},
],
},
{
name: "JSON Repair: single quotes to double quotes",
input: "{'name': 'John', 'age': 30}",
expectedOutput: "{\"name\": \"John\", \"age\": 30}",
recipeConfig: [
{
op: "JSON Repair",
args: [],
},
],
},
{
name: "JSON Repair: trailing comma in object",
input: "{\"name\": \"John\", \"age\": 30,}",
expectedOutput: "{\"name\": \"John\", \"age\": 30}",
recipeConfig: [
{
op: "JSON Repair",
args: [],
},
],
},
{
name: "JSON Repair: trailing comma in array",
input: "[1, 2, 3,]",
expectedOutput: "[1, 2, 3]",
recipeConfig: [
{
op: "JSON Repair",
args: [],
},
],
},
{
name: "JSON Repair: Python constants",
input: "{\"active\": True, \"data\": None, \"flag\": False}",
expectedOutput: "{\"active\": true, \"data\": null, \"flag\": false}",
recipeConfig: [
{
op: "JSON Repair",
args: [],
},
],
},
{
name: "JSON Repair: line comments",
input: `{
"name": "John", // This is a comment
"age": 30
}`,
expectedOutput: `{
"name": "John",
"age": 30
}`,
recipeConfig: [
{
op: "JSON Repair",
args: [],
},
],
},
{
name: "JSON Repair: block comments",
input: `{
"name": "John", /* This is a
multi-line comment */
"age": 30
}`,
expectedOutput: `{
"name": "John",
"age": 30
}`,
recipeConfig: [
{
op: "JSON Repair",
args: [],
},
],
},
{
name: "JSON Repair: missing comma",
input: `{
"name": "John"
"age": 30,
"city": "Boston"
}`,
expectedOutput: `{
"name": "John",
"age": 30,
"city": "Boston"
}`,
recipeConfig: [
{
op: "JSON Repair",
args: [],
},
],
},
{
name: "JSON Repair: complex mixed issues",
input: `{
name: "John", // Person's name
age: 30,
active: True,
data: None,
}`,
expectedOutput: `{
"name": "John",
"age": 30,
"active": true,
"data": null
}`,
recipeConfig: [
{
op: "JSON Repair",
args: [],
},
],
},
{
name: "JSON Repair: JSONP notation",
input: "callback({\"name\": \"John\", \"age\": 30})",
expectedOutput: "{\"name\": \"John\", \"age\": 30}",
recipeConfig: [
{
op: "JSON Repair",
args: [],
},
],
},
]);
Loading