-3

JSONDecoder().decode fails when there are commas at the end of a name field. Why is it happening? How can I solve it?

    let string = "[{\"name\":\"Homeoffice Marc,\",\"Km\":846.7911330652549,\"Strid\": \"DE262B62633E34AAA8A622E189B42920B319C371\"},{\"name\":\"Küche,\",\"Km\":857.8694764184313,\"Strid\": \"BD1A60D736BE86377121A2CC713251DBE2603BD5\"},{\"name\":\"Besprechungszimmer,\",\"Km\":857.8721480885644,\"Strid\": \"751A10C5D3065F91CC9F5BDF5E7111DC452D1C39\"},{\"name\":\"Büro Vertrieb,\",\"Km\":857.8723091979339,\"Strid\": \"148F97F324BB59EAFF613A0EB3766E026CFAB320\"},{\"name\":\"Büro Produktmanagement und Support,\",\"Km\":857.8734889037903,\"Strid\": \"3A37C955F7D3C012577B1D19B6F662AD233372A5\"},{\"name\":\"Tischkicker,\",\"Km\":857.8748603133218,\"Strid\": \"B5B8A86BBA2102AF56721166D2E814736EF13132\"},{\"name\":\"Büro Entwicklung,\",\"Km\":857.8773683652697,\"Strid\": \"E6814BE03EEF386E63AD7609D970BD9BA8CE71AD\"},{\"name\":\"Syfit GmbH,\",\"Km\":857.877841443768,\"Strid\": \"64F80B1EC04D008E060F28D7F198A8C39DCD53B5\"},{\"name\":\"Büro Zolti,\",\"Km\":857.8798725612223,\"Strid\": \"23F4C2E1C467AEC9D55D873DC3ED7FC73CD92177\"},{\"name\":\"Globale Suche\",\"Km\":null,\"Km\":846.7911330652549}]"
    
let data = string.data(using: .utf8) ?? Data()
let areas = try? JSONDecoder().decode([AreaModel].self, from: data)

decoding this data returns nil

My model:

struct AreaModel: Codable {
    enum CodingKeys: String, CodingKey {
        case name
        case km = "Km"
        case strid = "Strid"
    }
    
    let name: String
    let km: Double?
    let strid: String
}
Karina
  • 5
  • 3

1 Answers1

3

Please don't ignore thrown errors.

If you can't debug yourself, NEVER USE try?. With more experience, I'd say that we tend to not use try?, but sometimes we do. But when we write try?, we are able to find an possible issue, ie debug if needed.

Let's do a proper try then, with a do/catch:

do {
    let areas = try JSONDecoder().decode([AreaModel].self, from: data)
    print(areas)
} catch {
    print("Error while decoding: \(error)") //and print error, not error.description which is for non-developer and will be missing important information
}

Which should output:

Error while decoding: 
keyNotFound(CodingKeys(stringValue: "Strid", intValue: nil),
                       Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 9", intValue: 9)], 
                       debugDescription: "No value associated with key CodingKeys(stringValue: \"Strid\", intValue: nil) (\"Strid\").", underlyingError: nil))

So, it's the 9th element of your JSON array that causes the issue. It can't decode properly Strid value. There is no valid String value.

Let's see... You have {"name":"Globale Suche","Km":null,"Km":846.7911330652549}. In fact you have no Strid key here. Did you meant {"name":"Globale Suche","Strid":null,"Km":846.7911330652549}? Even if that's the case it's null, in other words, in Swift it would be nil. So to handle that case, you need to make Strid value optional

let strid: String?

Side note, the start of the answer if taken for another question of mine, so clearly not plagiarism.

Larme
  • 24,190
  • 6
  • 51
  • 81