Hi,
I am getting the following error
Error in decoding: typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "study_ref", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "study_state", intValue: nil)], debugDescription: "Expected to decode String but found a dictionary instead.", underlyingError: nil))
My code is
struct StudyDetails: Codable {
let id: String?
let object_type: String?
let state: String?
let title: String?
let brief_summary: String?
let detailed_description: String?
let start_date: String?
let end_date: String?
let study_type: String?
let time_frame: String?
let measure: String?
let enrollment: String?
let sponsor: String?
//let condition_ref: [String]?
let sampling_method: String?
let criteria: String?
let gender: String?
let minimum_age: Int?
let maximum_age: Int?
enum CodingKeys: String, CodingKey {
case object_type, state, title, brief_summary, detailed_description, start_date, end_date, study_type, time_frame, measure, enrollment, sponsor, sampling_method, criteria, gender, minimum_age, maximum_age
case id = "_id"
}
}
struct StudyRef: Codable {
let study_id: String?
var study_state: String?
let study_details: StudyDetails?
enum CodingKeys: String, CodingKey {
case study_id, study_state, study_details
}
}
struct StudyData: Codable {
let _id: String?
let object_type: String?
let age: String?
let participant_id: String?
let date_informed_consent: String?
let sex: String?
let race: String?
let _etag: String?
let study_ref: [StudyRef]?
enum CodingKeys: String, CodingKey {
case _id, object_type, age, participant_id, date_informed_consent, sex, race, _etag, study_ref
}
}
Json is
{
"_id": {
"$oid": "61886d21d91087e07b6f6aff"
},
"object_type": "patient",
"age": "52",
"participant_id": "YYj7zeese8gerbJcNO9Nx8vWnPL2",
"date_informed_consent": "{"$date":'2021-08-17T05:00:00.000Z'}",
"sex": "Male",
"race": "Unknown",
"_etag": {
"$oid": "61886d208f51ea289d3d70ef"
},
"study_ref": [
{
"study_id": "61833a654648a1695aff845d",
"study_state": "INCLUDED"
}
]
}
Study Details are not defined in the JSON data that I am working with. Is the error because of that?