Faheem Rahman
Faheem Rahman

Reputation: 360

Why do I get ‘Cannot get keyed decoding container -- found null value instead.’ error when I use JSONDecoder

(unknown context at $10741e078).CodingKeys>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Cannot get keyed decoding container -- found null value instead.", underlyingError: nil)

my model is ,

struct Details : Decodable {

    var userInfo : RequestUserInfo
    var commercialInfo : String?

}

struct RequestUserInfo : Decodable {

    var UserName : String?
    var Email : String?
    var UserIdentity : String?
    var UserMobile : String?
    var ThirdName : String?
    var LastName : String?
    var IdIssueDate : String?
    var IdSourceCity : String?


}

(unknown context at $10741e078).CodingKeys>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Cannot get keyed decoding container -- found null value instead.", underlyingError: nil)

Upvotes: 6

Views: 12065

Answers (4)

Adam Smith
Adam Smith

Reputation: 209

If you are JSON decoding to a Codable object mapped to Core Data then you can use a do try method. For example with an object which may have a set of names.

    do {
        names = NSSet(array: try 
        container.decode([Name].self, forKey: .names))
    } catch {
        print("no names found")
    }

If no names are within the JSON then no names found will be printed and the JSON decoder won't return an error

Upvotes: 0

In your case this may happens if Details.userInfo == nil

set

var userInfo : RequestUserInfo?

And your error will disappear. But in fact your error hides in wrong data. Try before decoding add string:

print(String(data:  data, encoding: .ascii))

and check carefully is anything is ok. For example, you may get "user_info" instead key "userInfo"

Upvotes: 1

PGDev
PGDev

Reputation: 24341

You need to mark the types as Optional (?) wherever you're expecting null in the JSON.

But, the issue with the above parsing is different from the one you described in the question.

typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "newRequest", intValue: nil), CodingKeys(stringValue: "parcels", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "UsingPurposeId", intValue: nil)], debugDescription: "Expected to decode String but found a number instead.", underlyingError: nil))

This can be resolved like so,

The type of UsingPurposeId in BuildingRequestParcelDetails must be Int? instead of String?

Parse the data in following way,

do {
    let response = try JSONDecoder().decode(BuildingRequestDetails.self, from: data)
    print(response)
} catch {
    print(error)
}

Upvotes: 9

Vadim Nikolaev
Vadim Nikolaev

Reputation: 2132

you can use this ModelAPI - https://app.quicktype.io?share=NYC0PgFnypeo5cLho3sH and call it with code self.requestDetail = try JSONDecoder().decode(ModelAPI.self, from: json). I've checked and it works. Also, I've fixed your JSON (error with two lines)

Upvotes: 0

Related Questions