I am trying to pull a list of a user's current plans from an API. The plans output formatted in JSON nested within an array and the content type is application/json; charset=utf-8.
[
{
"uwp_id": 1,
"user_id": 77,
"plan_id": 1,
"date_joined": "0000-00-00 00:00:00",
"name_of_plan": "Trip to New York!",
"head_planner_id": null,
"trip_start_date": null,
"trip_end_date": null,
"location": "New York",
"notes": null
},
{
"uwp_id": 2,
"user_id": 77,
"plan_id": 2,
"date_joined": "0000-00-00 00:00:00",
"name_of_plan": "Trip to India!",
"head_planner_id": null,
"trip_start_date": null,
"trip_end_date": null,
"location": "New Delhi, India",
"notes": null
}
]
However, when attempting to decode it in Swift using the decoder, I am receiving the following error:
dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))
My data model is structured as such:
struct ListOfUsersCurrentPlans: Decodable {
var IndividualPlan: [IndividualPlan]
}
struct IndividualPlan : Decodable {
var uwp_id: Int?
var user_id: Int?
var plan_id: Int?
var date_joined: String?
var name_of_plan: String?
var head_planner_id: Int?
var trip_start_date: String?
var trip_end_date: String?
var location: String?
var notes: String?
}
And here is what my decoder line of code looks like:
let parsedData: ListOfUsersCurrentPlans = try decoder.decode(ListOfUsersCurrentPlans.self, from: data!)
I am not sure what the issue seems to be, I have tried to restructure the data model numerous times as well.