diff --git a/src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java b/src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java index c63ac02..8f37380 100644 --- a/src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java +++ b/src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java @@ -141,25 +141,50 @@ public class BirthAndDeathDetailsService { // } - private String sanitizeJsonResponse(String rawJson) { - if (rawJson == null) return ""; - - return rawJson - .replace("\\", "\\\\") // Escape backslashes - .replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "") // Remove non-printable control characters - .replaceAll("\t", "") // Remove tab characters - .replaceAll("\\s*:\\s*\n\\s*\"", ":\"") // Fix broken key-value splits - .replaceAll("\r", "") // Remove carriage return - .replaceAll("\n", "") // Remove newlines - .replaceAll(",\\s*\\[", "[") // Remove comma before array start - .replaceAll("\\[\\s*,", "[") // Remove comma after array start - .replaceAll(",\\s*\\]", "]") // Remove trailing comma in array - .replaceAll("\"\"\"", "\"\"") // ← fix triple quotes - .replaceAll("\"{3,}", "\"") // ← fallback: collapse 3+ quotes to 1 - .replaceAll(":(\\s*)\"{2,}\"", ":\"\""); // ← handle `:"""` to `:""` - } + // private String sanitizeJsonResponse(String rawJson) { + // if (rawJson == null) return ""; + + // return rawJson + // .replace("\\", "\\\\") // Escape backslashes + // .replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "") // Remove non-printable control characters + // .replaceAll("\t", "") // Remove tab characters + // .replaceAll("\\s*:\\s*\n\\s*\"", ":\"") // Fix broken key-value splits + // .replaceAll("\r", "") // Remove carriage return + // .replaceAll("\n", "") // Remove newlines + // .replaceAll(",\\s*\\[", "[") // Remove comma before array start + // .replaceAll("\\[\\s*,", "[") // Remove comma after array start + // .replaceAll(",\\s*\\]", "]") // Remove trailing comma in array + // .replaceAll("\"\"\"", "\"\"") // ← fix triple quotes + // .replaceAll("\"{3,}", "\"") // ← fallback: collapse 3+ quotes to 1 + // .replaceAll(":(\\s*)\"{2,}\"", ":\"\"") // ← handle `:"""` to `:""` + // .replaceAll("\"\\s*\\(", " ("); // Remove quote before parenthesis + + // } + private String sanitizeJsonResponse(String rawJson) { + if (rawJson == null) return ""; + + // Remove quote before parenthesis (fixes: "NAME" : "ABC "(LATE)") + String fixed = rawJson.replaceAll("\"\\s*\\(([^)]*)\\)\"", " ($1)\""); + + // Remove parenthesis and content outside of quotes (fixes: ... "ABC" (LATE) ... → ... "ABC" ...) + fixed = fixed.replaceAll("\"\\s*\\([^)]*\\)", "\""); + + return fixed + .replace("\\", "\\\\") // Escape backslashes + .replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "") // Remove non-printable control characters + .replaceAll("\t", "") // Remove tab characters + .replaceAll("\\s*:\\s*\n\\s*\"", ":\"") // Fix broken key-value splits + .replaceAll("\r", "") // Remove carriage return + .replaceAll("\n", "") // Remove newlines + .replaceAll(",\\s*\\[", "[") // Remove comma before array start + .replaceAll("\\[\\s*,", "[") // Remove comma after array start + .replaceAll(",\\s*\\]", "]") // Remove trailing comma in array + .replaceAll("\"\"\"", "\"\"") // fix triple quotes + .replaceAll("\"{3,}", "\"") // collapse 3+ quotes to 1 + .replaceAll(":(\\s*)\"{2,}\"", ":\"\""); // handle `:"""` to `:""` +}