json-restructure
Version:
A TypeScript library for repairing malformed JSON strings
33 lines • 1.64 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.repairJson = exports.validateJson = exports.parseJson = exports.fixJson = void 0;
const jsonParser_1 = require("./utils/jsonParser");
Object.defineProperty(exports, "parseJson", { enumerable: true, get: function () { return jsonParser_1.parseJson; } });
const jsonValidator_1 = require("./utils/jsonValidator");
Object.defineProperty(exports, "validateJson", { enumerable: true, get: function () { return jsonValidator_1.validateJson; } });
const jsonRepair_1 = require("./utils/jsonRepair");
Object.defineProperty(exports, "repairJson", { enumerable: true, get: function () { return jsonRepair_1.repairJson; } });
function fixJson(jsonString) {
try {
// Attempt to parse the JSON string
const parsedJson = (0, jsonParser_1.parseJson)(jsonString);
// If the JSON is valid, return the original string
if ((0, jsonValidator_1.validateJson)(jsonString)) {
return jsonString;
}
// If the JSON is malformed, attempt to repair it
const repairedJson = (0, jsonRepair_1.repairJson)(jsonString);
// If the repair was successful, return the repaired JSON
if ((0, jsonValidator_1.validateJson)(repairedJson)) {
return repairedJson;
}
// If the repair failed, throw an error
throw new Error('Failed to repair malformed JSON');
}
catch (error) {
// If an error occurs during parsing or validation, throw it
throw error;
}
}
exports.fixJson = fixJson;
//# sourceMappingURL=index.js.map
;