Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a Decodable for a JSON in Swift 4, where keys are dynamic?

I've a JSON like this.

I need to make a corresponding Decodable struct in my iOS app using Swift 4.

{
    "cherry": {
        "filling": "cherries and love",
        "goodWithIceCream": true,
        "madeBy": "my grandmother"
     },
     "odd": {
         "filling": "rocks, I think?",
         "goodWithIceCream": false,
         "madeBy": "a child, maybe?"
     },
     "super-chocolate": {
         "flavor": "german chocolate with chocolate shavings",
         "forABirthday": false,
         "madeBy": "the charming bakery up the street"
     }
}

Need help on making the Decodable Struct. How to mention the unknown keys like cherry,odd and super-chocolate.

like image 604
iranjith4 Avatar asked Mar 08 '23 03:03

iranjith4


1 Answers

What you need is to get creative in defining the CodingKeys. Let's call the response a FoodList and the inner structure FoodDetail. You haven't defined the properties of FoodDetail so I assume that the keys are all optional.

struct FoodDetail: Decodable {
    var name: String!
    var filling: String?
    var goodWithIceCream: Bool?
    var madeBy: String?
    var flavor: String?
    var forABirthday: Bool?

    enum CodingKeys: String, CodingKey {
        case filling, goodWithIceCream, madeBy, flavor, forABirthday
    }
}

struct FoodList: Decodable {
    var foodNames: [String]
    var foodDetails: [FoodDetail]

    // This is a dummy struct as we only use it to satisfy the container(keyedBy: ) function
    private struct CodingKeys: CodingKey {
        var intValue: Int?
        var stringValue: String

        init?(intValue: Int) { self.intValue = intValue; self.stringValue = "" }
        init?(stringValue: String) { self.stringValue = stringValue }
    }

    init(from decoder: Decoder) throws {
        self.foodNames = [String]()
        self.foodDetails = [FoodDetail]()

        let container = try decoder.container(keyedBy: CodingKeys.self)
        for key in container.allKeys {
            let foodName = key.stringValue
            var foodDetail = try container.decode(FoodDetail.self, forKey: key)
            foodDetail.name = foodName

            self.foodNames.append(foodName)
            self.foodDetails.append(foodDetail)
        }
    }
}


// Usage
let list = try! JSONDecoder().decode(FoodList.self, from: jsonData)
like image 186
Code Different Avatar answered Mar 10 '23 17:03

Code Different