0

My process is I have bunch of json string that I need to convert into request payload and make a POST request to api call. The issue is key may or may not exist from my original json string and the API does not accept nil as value. So the API call failed after decode if the key does not exist. The snippet code from mine is like following I transfer into decoable type. I search online and found the way decodeIfPresent in swift5 How can I do it?

struct Student: Codable {
    var name: String
    var skill: String?

    public enum Student: String, CodingKey {
        case name
        case skill
    }

    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: Student.self)
        self.name = try container.decode(String.self, forKey: .name)
        self.skill = try container.decodeIfPresent(String.self, forKey: .skill)
    }
}
let testString = "{\"name\": \"withSkill\", \"skill\": \"yell\"}"
let testStringB = "{\"name\": \"no Skill\"}"
let data = testString.data(using: .utf8)!
let dataB = testStringB.data(using: .utf8)!
let decodeData = try? JSONDecoder().decode(Student.self, from: data)
let decodeDataB = try? JSONDecoder().decode(Student.self, from: dataB)
/// what I get
▿ Optional<Student>
    ▿ some : Student
        - name : "no Skill"
        - skill : nil

/// what I want
▿ Optional<Student>
    ▿ some : Student
        - name : "no Skill"
jacobcan118
  • 7,797
  • 12
  • 50
  • 95
  • It's unclear what you want. Do you understand that once you declared the `Student` struct like that, then it must have a `skill` property, and it must have a value? (nil or otherwise) – Sweeper Oct 13 '22 at 19:30
  • since my request json may or may not have key `skill` without declaring as optional then how should i do it? – jacobcan118 Oct 13 '22 at 19:43
  • What's wrong with declaring as optional? What's wrong with `skill` being nil? – Sweeper Oct 13 '22 at 19:49
  • The request does not accept value as nil. So if the value is nil I have to filter out the key – jacobcan118 Oct 13 '22 at 23:46
  • "The request does not accept value as nil. So if the value is nil I have to filter out the key – " What request? Are you more interested in Encoding? If so when a value is nil, you can decide to not put it into the JSON. – Larme Oct 14 '22 at 07:54
  • @Larme, my process is I have bunch of json string that I need to convert into request payload and make a post request to api call. m tissue here is key may or may not exist from my original json string. the api does not accept nil so call failed after decode if the key does not exist – jacobcan118 Oct 14 '22 at 11:47
  • https://stackoverflow.com/questions/58809868/how-to-remove-data-model-nil-fields-from-custom-encoded-decoded-json-in-swift ? So it's about encoding, not decoding... – Larme Oct 14 '22 at 13:09
  • hm. how the above is different than I have? – jacobcan118 Oct 14 '22 at 15:04
  • Your issue is about sending to an API encoded JSON where you ignore keys which have `nil` values. Then you need to focus on `func encode(to encoder: Encoder)`, not `decode(from:)`... – Larme Oct 15 '22 at 19:25
  • @jacobcan118 Actually, the default encode implementation automatically discards properties with type `nil`, so they don't appear in the data. Does the behaviour you're seeing not match that? – CryptoAlgorithm Nov 16 '22 at 05:30

0 Answers0