Phonebook challenge datafile

Have an error with parsing the phonebook datafile ; < Couldn’t parse the JSON keyNotFound(CodingKeys(stringValue: “id”, intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: “Index 0”, intValue: 0)], debugDescription: “No value associated with key CodingKeys(stringValue: "id", intValue: nil) ("id").”, underlyingError: nil)) > All I could find online that an enum for coding keys is required Tried coding keys but still get an error for null value
Any direction is appreciated
Thanks Joel

Can you post your data model definitions that relate to the json file. ie, those that define the Response, Company, Department and Employee.

A good tool to use to arrive at the correct data definition is https://app.quicktype.io

Hi Chris, thanks for the quick reply. Have a great holiday !
Below are the model definitions;

struct Company: Identifiable , Decodable {

var id = UUID()
var companyName: String
var departments: [Department]

}
struct Department: Identifiable , Decodable {

var id =  UUID()
var department_name : String
var employee: [Employee]

}
struct Employee: Identifiable , Decodable {

var id = UUID()
var name: String
var position: String
var email: String
var phone: String

}
Great tool - generated models but still an error eg;
// MARK: - Company

struct Company: Codable { *** Type ‘Company’ does not conform to protocol ‘Encodable’**

let companyName: String

let departments: [Department]

enum CodingKeys: String, CodingKey {

case companyName = “company_name”

case departments

}

}

If you want to have the names of the properties comply with camel case (which is the Swift way) then you will have to add CodingKeys to all three structs even if they don’t require the names of the properties to be aligned with the Swift standard (as is the case with Employee).

For example in the Employee struct you would do this:

// MARK: - Employee
struct Employee: Identifiable, Codable {
    var id = UUID()
    var name: String
    var position: String
    var email: String
    var phone: String

    enum CodingKeys: String, CodingKey {
        case name, position, email, phone
    }
}

The QuickType tool does not include CodingKeys for the Employee so while it is a good tool it’s not perfect.

Thanks Chris