Last active
November 16, 2023 09:03
-
-
Save malcommac/0ecb4d22877b346b7ace0758d83c4093 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isValidJSON(_ jsonString: String) -> Bool { | |
if let data = jsonString.data(using: .utf8) { | |
do { | |
_ = try JSONSerialization.jsonObject(with: data, options: []) | |
return true | |
} catch { | |
return false | |
} | |
} | |
return false | |
} | |
let writtenFileData = try! String(contentsOfFile: file.path) | |
let fileIsMalformed = isValidJSON(writtenFileData) | |
if fileIsMalformed { | |
let corruptedFileURL = URL(fileURLWithPath: "<PATH>/segment_\(UUID().uuidString).json") | |
try! writtenFileData.write(to: corruptedFileURL, atomically: true, encoding: .utf8) | |
print("### SEGMENT ### Batch file is not valid json") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment