0

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.

Hilardy
  • 1
  • 2
  • 5
  • Check what the potentially invisible first character is of that string. I bet it is not "[". – trincot Feb 19 '22 at 04:55
  • @trincot how would I check? The JSON above is what my HTTP client outputs – Hilardy Feb 19 '22 at 04:57
  • `str[0].unicodeScalarCodePoint()` See [here](https://stackoverflow.com/questions/24102044/how-can-i-get-the-unicode-code-points-of-a-character), `str.utf8`... – trincot Feb 19 '22 at 05:00
  • @trincot wasnt sure how to implement that but I checked the length of the data and it's not the length of my JSON object... – Hilardy Feb 19 '22 at 05:16
  • 1
    *Invalid value around character 0* means that the received data is HTML rather than JSON which indicates a server error. Print the data with `print(String(data: data, encoding: .utf8)!)`. Furthermore your model doesn't match the JSON anyway. – vadian Feb 19 '22 at 05:19

1 Answers1

0

Found the error....

I put "Authentication" instead of "Authorization" in the forHTTPHeaderField parameter for the request.setValue method.

Hilardy
  • 1
  • 2
  • 5