0

My scenario, I am trying to create codable structure for JSON response but I am getting “Type 'Datum' does not conform to protocol 'Decodable' “ error. I made some mistakes in codable but I can’t able to find it. Please check below my response and code and give me the solution.

My Response

{
    "status": 200,
    "message": "Success",
    "country": [
        {
            "id": 1,
            "master": "India",
            "type": 2,
            "active": 1
        },
        {
            "id": 2,
            "master": "US",
            "type": 2,
            "active": 1
        }
    ],
    "cost": 13764,
    "data": [
        {
            "id": 1,
            "user_id": 167,
            "country": 1,
            "card": 1,
            "category": 4,
            "title": “kms”,
            "description": “check”,
            "cost": 444,
            "attachment": "sample.png",
            "create_date": "2019-02-13T00:00:00.000Z",
            "device_id": "1111",
            "app_type": "Android",
            "location": “USA”,
            "user": {
                "firstname": “Mike”,
                "lastname": "P"
            },
            "app_trans_card": {
                "card": “012”
            },
            "app_trans_master": {
                "master": "Domain"
            }
        }
   ]
}

My Code

struct Root: Codable {
    let status: Int
    let message: String
    let cost: Int
    let data: [Datum]

    enum CodingKeys: String, CodingKey {
        case status
        case message = "message"
        case cost
        case data
    }
}

struct Datum: Codable {
    let id, user_id, country, card, category, cost: Int
    let title: String
    let description: String
    let attachment: String
    let create_date: String
    let device_id: String
    let app_type: String
    let location: String

    let user: Fullname
    let app_trans_card: TransactionCard
    let app_trans_master: TransactionMaster

    enum CodingKeys: String, CodingKey {
        case id = "id"
        case user_id = "user_id"
        case country = "country"
        case card = "card"
        case category = "category"
        case cost = "cost"
        case title, description, attachment, create_date, device_id, app_type, location, fullname, transactionCard, transactionMaster
    }
}

struct Fullname: Codable {
    let firstname, lastname: String
}

struct TransactionCard: Codable {
    let card: String
}

struct TransactionMaster: Codable {
    let master: String
}
1
  • Make sure you are getting let id, user_id, country, card, category, cost: Int values as Int Please check making those param as String Commented Feb 18, 2019 at 6:48

1 Answer 1

1

I have found some problem if you JSON which is related with at many places and it should be " so first correct that and then your JSON will be:

{
    "status": 200,
    "message": "Success",
    "country": [
        {
            "id": 1,
            "master": "India",
            "type": 2,
            "active": 1
        },
        {
            "id": 2,
            "master": "US",
            "type": 2,
            "active": 1
        }
    ],
    "cost": 13764,
    "data": [
        {
            "id": 1,
            "user_id": 167,
            "country": 1,
            "card": 1,
            "category": 4,
            "title": "kms",
            "description": "check",
            "cost": 444,
            "attachment": "sample.png",
            "create_date": "2019-02-13T00:00:00.000Z",
            "device_id": "1111",
            "app_type": "Android",
            "location": "USA",
            "user": {
                "firstname": "Mike",
                "lastname": "P"
            },
            "app_trans_card": {
                "card": "012"
            },
            "app_trans_master": {
                "master": "Domain"
            }
        }
   ]
}

Now you can create your Codable protocol with THIS site and code it will look like:

struct Root: Codable {
    let status: Int
    let message: String
    let country: [Country]
    let cost: Double
    let data: [Datum]
}

struct Country: Codable {
    let id: Int
    let master: String
    let type, active: Int
}

struct Datum: Codable {
    let id, userID, country, card: Int
    let category: Int
    let title, description: String
    let cost: Double
    let attachment, createDate, deviceID, appType: String
    let location: String
    let user: User
    let appTransCard: AppTransCard
    let appTransMaster: AppTransMaster

    enum CodingKeys: String, CodingKey {
        case id
        case userID = "user_id"
        case country, card, category, title, description, cost, attachment
        case createDate = "create_date"
        case deviceID = "device_id"
        case appType = "app_type"
        case location, user
        case appTransCard = "app_trans_card"
        case appTransMaster = "app_trans_master"
    }
}

struct AppTransCard: Codable {
    let card: String
}

struct AppTransMaster: Codable {
    let master: String
}

struct User: Codable {
    let firstname, lastname: String
}
Sign up to request clarification or add additional context in comments.

8 Comments

Great @Dharmesh Kheni. Thank you.
Don't forget to accept the answer if it helps... :)
I am getting dataCorrupted(Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "cost", intValue: nil)], debugDescription: "Parsed JSON number <1283.5> does not fit in Int.", underlyingError: nil)) @Dharmesh Kheni
Show your JSON
Changed cost Int to Double everything working fine now :). Thanks dude
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.