0

I'm using Swift 5 and Xcode 11. I am attempting to parse the JSON coming back from this website, an API I am designing. http://aarontcraig-001-site1.htempurl.com/api/values

The JSON returned from that API call is this:

[
    {
        "Businesses": [],
        "Customer": null,
        "ID": 1,
        "Name": "Coffee Shop",
        "CustomerID": null
    },
...
]

It continues an array. Some of the entries are null, others are not. However, when I parse it, they all come back nil.

Here is my code:

let url = "http://aarontcraig-001-site1.htempurl.com/api/values"
        guard let finalURL = URL(string: url) else {
            return
        }

        URLSession.shared.dataTask(with: finalURL) { (data, response, error) in
            guard let data = data else {return}

            do {
                let myData = try JSONDecoder().decode([Value].self, from: data)

                print(myData)
            }
            catch {
                print("Error parsing \(error)")
            }

and the struct I am using to catch it all:

struct Value: Codable {
    let businesses : [String]?
    let customer : String?
    let iD : Int?
    let name : String?
    let customerID : String?
}

All I get back is nil values, even though clearly many of them aren't. This is what it returns.

[Sunrise.Value(businesses: nil, customer: nil, iD: nil, name: nil, customerID: nil), Sunrise.Value(businesses: nil, customer: nil, iD: nil, name: nil, customerID: nil), Sunrise.Value(businesses: nil, customer: nil, iD: nil, name: nil, customerID: nil), Sunrise.Value(businesses: nil, customer: nil, iD: nil, name: nil, customerID: nil), Sunrise.Value(businesses: nil, customer: nil, iD: nil, name: nil, customerID: nil), Sunrise.Value(businesses: nil, customer: nil, iD: nil, name: nil, customerID: nil)]

What am I doing wrong? Even if I attempt to just capture name which has a value for each entry, it sets it to nil. I am receiving the data, because if I put a breakpoint at data I can see it there.

ATCraiger
  • 109
  • 2
  • 13
  • 1
    Keys don't match. "CustomerID" != "customerID". Don't make them Optional, you'll see. I think that `id` shouldn't never be nil (I hope for the model). See https://stackoverflow.com/questions/44396500/how-do-i-use-custom-keys-with-swift-4s-decodable-protocol – Larme Apr 23 '20 at 21:12
  • Does this answer your question? [How do I use custom keys with Swift 4's Decodable protocol?](https://stackoverflow.com/questions/44396500/how-do-i-use-custom-keys-with-swift-4s-decodable-protocol) – Larme Apr 23 '20 at 21:15

1 Answers1

2

Map the property names from the JSON to your struct's members using CodingKeys:

struct Value: Codable {
    let businesses: [String]?
    let customer: String?
    let id: Int
    let name: String
    let customerID: String?

    enum CodingKeys: String, CodingKey {
        case businesses = "Businesses"
        case customer = "Customer"
        case id = "ID"
        case name = "Name"
        case customerID = "CustomerID"
    }
}
Gereon
  • 17,258
  • 4
  • 42
  • 73