0
votes

I have a JSON response that I am parsing with the help of codable models.

Even though my models look good, I am getting the following error:

[Result]: FAILURE: keyNotFound(CodingKeys(stringValue: “user”, intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: “No value associated with key CodingKeys(stringValue: \“user\“, intValue: nil) (\“user\“).“, underlyingError: nil))

The JSON response is:

{
   “status”: “success”,
   “message”: “successfully.“,
   “user”: {
       “username”: “admin”,
       “profileImage”: “/storage/default.png”
   },
   “data”: {
       “cash”: {
           “withdrawableCash”: “$999540”,
           “outstandingOrders”: “$0”
       },
       “offering”: [
           {
               “company”: “ABCD”,
               “location”: “San Francisco, CA”,
               “amount”: 10
           }
       ],
       “history”: [
           {
               “amount”: 330,
               “order_id”: 3,
               “order_type”: “preBid”,
               “status”: 2,
               “message”: “Placed bid with 33 notes, on Auction,
               “transaction_time”: “31-07-2018 05:31"
           }
       ]
   }
}

The models are:

public struct WalletResponseModel: Codable {
    public let status: String
    public let message: String
    public let user: UserData
    public let data: WalletData
}

public struct UserData: Codable {
    public let username: String
    public let profileImage: URL

    enum CodingKeys: String, CodingKey {
        case username
        case profileImage = "profileImage"
    }
}

public struct WalletData: Codable {
    public let cash: Cash
    public let history: [HistoryItem]
    public let offerings: [Offering]

    enum CodingKeys: String, CodingKey {
        case cash
        case history
        case offerings = "offering"
    }
}

public struct Cash: Codable {
    public let withdrawableCash: String
    public let outstandingOrders: String
}

public struct HistoryItem: Codable {
    public let amount: Int
    public let status: Int
    public let orderId: Int
    public let orderType: String
    public let message: String
    public let transactionTime: String

    enum CodingKeys: String, CodingKey {
        case amount, status, message
        case transactionTime = "transaction_time"
        case orderId = "order_id"
        case orderType = "order_type"
    }
}

public struct Offering: Codable {
    public let company: String
    public let amount: Int
    public let location: String
}
1

1 Answers

1
votes

Your json format is incorrect it contains “ ” instead of " " , the valid one is

{
    "status": "success",
    "message": "successfully.",
    "user": {
        "username": "admin",
        "profileImage": "/storage/default.png"
    },
    "data": {
        "cash": {
            "withdrawableCash": "$999540",
            "outstandingOrders": "$0"
        },
        "offering": [{
            "company": "ABCD",
            "location": "San Francisco, CA",
            "amount": 10
        }],
        "history": [{
            "amount": 330,
            "order_id": 3,
            "order_type": "preBid",
            "status": 2,
            "message": "Placed bid with 33 notes, on Auction",
            "transaction_time": "31-07-2018 05:31"
        }]
    }
}