I am trying to create decodable structs for the JSON that I am receiving, but I keep getting:
keyNotFound(CodingKeys(stringValue: "inquiry_quantity", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "orders", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys...
This is the JSON that I am running through my decoder:
{
"orders_important": 2,
"orders_inquiry": 2,
"orders": [
{
"information": {
"total_price": 12.0,
"order_stage": "CONFIRMED",
"total_cases": 333,
"scanned": false,
"inquiry": false,
"foodhub": "[email protected]",
"identifier": "GPO00024",
"delivery_date": "2019-11-04"
},
"details": {
"pack_size": "40#",
"cases": 333,
"plu_code": 12434,
"crop_plan": 1633,
"commodity": "Conventional Bananas, , 40#"
}
},
{
"information": {
"inquiree": "FARMER",
"total_price": 23.5,
"order_stage": "INQUIRY",
"inquiry_quantity": 0,
"total_cases": 56,
"scanned": false,
"inquiry": true,
"foodhub": "[email protected]",
"inquiry_type": "PRICE",
"identifier": "GPO00027",
"inquiry_price": 26.5,
"delivery_date": "2019-11-05",
"inquiry_details": "You have requested $26.5 per case, instead of $23.5."
},
"details": {
"pack_size": "40#",
"cases": 12,
"plu_code": 12434,
"crop_plan": 1589,
"commodity": "Conventional Bananas, , 40#"
}
},
],
"orders_count": 4
}
My decodable structs: (EDIT: Removed Init)
struct OrderBundle: Decodable{
let orders_important : Int
let orders_inquiry:Int
let orders_count: Int
let orders: [Order]
}
struct Order: Decodable {
let inquiry_quantity : Int
let inquiry_price : Int
let inquiry_info : String
let price : Int
let cropPlan : Int
let identifier: String
let deliveryDate : String
let quantity : Int
let unit : String
let status : String
let destination : String
let commodity: String
}
And lastly calling my decoder:
let orderData = try
JSONDecoder().decode(OrderBundle.self, from: data!)
print(orderData)
I have tried it with and without the Init in my Orders and both ways I am still getting the same error.
init(json:)won't be called byJSONDecoder().decode(OrderBundle.self, from: data!), you know that? You need to add a custom init with coder to handle the same. - LarmeOrderappear directly inthe json. THey're nested within two sub-objected, keyed by"information"and"details". You either need to make anOrderInformationandOrderDetailsstruct, or you need to write a custom initializer that parses that data out to populate directly into yourOrder. medium.com/@nictheawesome/… - Alexander