If you open a play ground and paste this code, you’ll notice that the decoded object is nil. The JSON object [{"transcript":"A person might say, "Hello"."}]
passes any linter successfully but it seems like the default JSON decoder in Swift has an issue with the escaped double quotes ("
) in the transcript. To make sure, in my playground when I remove the two escaped double quotes from the string, it works fine.
var testData2 = """
[{"transcript":"A person might say, "Hello"."}]
"""
struct TranscriptModel: Codable, Identifiable {
let id = UUID()
let transcript : String
}
typealias Transcripts = [TranscriptModel]
let decodedResponse1 = try? JSONDecoder().decode(Transcripts.self, from: Data(testData2.utf8))
print(decodedResponse1)
It seems like a bug in Swift’s JSON decoder? Is there a work around so that I can show double quotes in my json string?
If used by itself within your code, swift recognizes the escaped double quote and works perfectly by showing the "
symbol as you can see in the picture below