Search code examples
jsonswiftdecodable

How to parse JSON using custom decoder init with incrementing keys in Swift


I have some meal json which I want to convert into a MealData struct

let randomMealJson = """
{
    "meals": [{
        "idMeal": "52812",
        "strMeal": "Beef Brisket Pot Roast",
        "strDrinkAlternate": null,
        "strCategory": "Beef",
        "strArea": "American",
        "strInstructions": "Meal instructions",
        "strMealThumb": "https://www.themealdb.com/images/media/meals/ursuup1487348423.jpg",
        "strTags": "Meat",
        "strYoutube": "https://www.youtube.com/watch?v=gh48wM6bPWQ",
        "strIngredient1": "Beef Brisket",
        "strIngredient2": "Salt",
        "strIngredient3": "Onion",
        "strIngredient4": "Garlic",
        "strIngredient5": "Thyme",
        "strIngredient6": "Rosemary",
        "strIngredient7": "Bay Leaves",
        "strIngredient8": "beef stock",
        "strIngredient9": "Carrots",
        "strIngredient10": "Mustard",
        "strIngredient11": "Potatoes",
        "strIngredient12": null,
        "strIngredient13": null,
        "strIngredient14": null,
        "strIngredient15": null,
        "strIngredient16": null,
        "strIngredient17": null,
        "strIngredient18": null,
        "strIngredient19": null,
        "strIngredient20": null,
        "strMeasure1": "4-5 pound",
        "strMeasure2": "Dash",
        "strMeasure3": "3",
        "strMeasure4": "5 cloves",
        "strMeasure5": "1 Sprig",
        "strMeasure6": "1 sprig ",
        "strMeasure7": "4",
        "strMeasure8": "2 cups",
        "strMeasure9": "3 Large",
        "strMeasure10": "1 Tbsp",
        "strMeasure11": "4 Mashed",
        "strMeasure12": "",
        "strMeasure13": "",
        "strMeasure14": "",
        "strMeasure15": "",
        "strMeasure16": "",
        "strMeasure17": "",
        "strMeasure18": "",
        "strMeasure19": "",
        "strMeasure20": "",
        "strSource": "http://www.simplyrecipes.com/recipes/beef_brisket_pot_roast/",
        "dateModified": null
    }]
}
"""

Here is the structure I want to make...

struct MealData: Decodable {
    let meals: [Meal]
}

struct Meal: Decodable {
    let items: [Item]
}

extension Meal {
    struct Item: Decodable {
        let ingredient: String
        let measure: String
    }
}

extension Meal {
    init(from decoder: Decoder) throws {
//        var items: [Item] = (0...20).map { num in      
//           what to do here?
//        }
    }
}

I'd like to go through the ingredients and measures and create a Meal.Item from them but I can't work out how to achieve this. It is possible in the way I have indicated in the code?


Solution

  • Following @joakim-danielson's answer. You can do the same thing inside init(from:) initializer, like your original intention.

    Just decode the ingredients and measures of each Meal as [String: String?] and compose your custom Items:

    extension Meal {
        init(from decoder: Decoder) throws {
            let container = try decoder.singleValueContainer()
            
            let mealDict = try container.decode([String: String?].self)
            var index = 1
            var items = [Item]()
            while
                let ingredient = mealDict["strIngredient\(index)"] as? String,
                let measure = mealDict["strMeasure\(index)"] as? String,
                !measure.isEmpty
            {
                items.append(Item(ingredient: ingredient, measure: measure))
                index += 1
            }
            self.items = items
        }
    }