Skip to content

Instantly share code, notes, and snippets.

@malcommac
Last active November 16, 2023 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcommac/0ecb4d22877b346b7ace0758d83c4093 to your computer and use it in GitHub Desktop.
Save malcommac/0ecb4d22877b346b7ace0758d83c4093 to your computer and use it in GitHub Desktop.
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