How do I access data in a nested json in Swift(while using optionals in my data model)

64 Views Asked by At

I have my struct as:

struct CategoryViewModel2: Codable {
    let data: [Datum2]?
}

// MARK: - Datum
struct Datum2: Codable {
    let the0: Int?
    let the1, the2: String?
    let the3, the4, id: Int?
    let name, image: String?
    let parentID, status: Int?
    let item: [Item2]?
    let sub: [Sub2]?

    enum CodingKeys: String, CodingKey {
        case the0 = "0"
        case the1 = "1"
        case the2 = "2"
        case the3 = "3"
        case the4 = "4"
        case id, name, image
        case parentID = "parent_id"
        case status, item, sub
    }
}

// MARK: - Item
struct Item2: Codable {
     let id: Int?
    let name: String?
    let image: String?
    let itemDescription: String?
    let price: Int?
    let saleprice: Int?
    let parentID, isVariation, ftype, status: Int?
    let type: String?
    let isCart: Int?
    let imageslider: [Imageslider2]?
    let isfavourite: Int?
    let variation: [Variation2]?

    enum CodingKeys: String, CodingKey {
       
        case id, name, image
        case itemDescription = "description"
        case price, saleprice
        case parentID = "parent_id"
        case isVariation = "is_variation"
        case ftype, status, type
        case isCart = "is_cart"
        case imageslider, isfavourite, variation
    }
}

// MARK: - Imageslider
struct Imageslider2: Codable {
    let the0, imagename: String?
    let imagepath: String?

    enum CodingKeys: String, CodingKey {
        case the0 = "0"
        case imagename, imagepath
    }
}

 

// MARK: - Variation
struct Variation2: Codable {
    let the0, the1: Int?
    let the2: String?
    let the3: Int?
    let the4: Int?
    let id, itemID: Int?
    let attributeValue: String?
    let variationPrice: Int?
    let variationSaleprice: Int?
    let isCart: Int?

    enum CodingKeys: String, CodingKey {
        case the0 = "0"
        case the1 = "1"
        case the2 = "2"
        case the3 = "3"
        case the4 = "4"
        case id
        case itemID = "item_id"
        case attributeValue = "attribute_value"
        case variationPrice = "variation_price"
        case variationSaleprice = "variation_saleprice"
        case isCart = "is_cart"
    }
}

And my json response is like this:

`{
    "data": [
        {
            "id": 10,
            "0": 10,
            "name": "Coffee",
            "1": "Coffee",
            "image": "28-11-2022-1669614108unnamed.png",
            "2": "28-11-2022-1669614108unnamed.png",
            "parent_id": 0,
            "3": 0,
            "status": 1,
            "4": 1,
            "item": []
        },
        {
            "id": 1,
            "0": 1,
            "name": "Soups",
            "1": "Soups",
            "image": "29-09-2022-1664432271Homemade-Vegetable-Soup-Recipe-2-1200.jpeg",
            "2": "29-09-2022-1664432271Homemade-Vegetable-Soup-Recipe-2-1200.jpeg",
            "parent_id": 0,
            "3": 0,
            "status": 1,
            "4": 1,
            "item": [
                {
                    "id": 2,
                    "0": 2,
                    "name": "Subzi Ka Shorba",
                    "1": "Subzi Ka Shorba",
                    "image": null,
                    "2": null,
                    "description": "Borth of mixed veggies",
                    "3": "Borth of mixed veggies",
                    "price": 15,
                    "4": 15,
                    "saleprice": 12,
                    "5": 12,
                    "parent_id": 1,
                    "6": 1,
                    "is_variation": 1,
                    "7": 1,
                    "ftype": 1,
                    "8": 1,
                    "status": 1,
                    "9": 1,
                    "type": "tm",
                    "10": "tm",
                    "variation": [
                        {
                            "id": 36,
                            "0": 36,
                            "item_id": 2,
                            "1": 2,
                            "attribute_value": "Full",
                            "2": "Full",
                            "variation_price": 13,
                            "3": 13,
                            "variation_saleprice": 11,
                            "4": 11,
                            "is_cart": 0
                        },
                        {
                            "id": 35,
                            "0": 35,
                            "item_id": 2,
                            "1": 2,
                            "attribute_value": "Half",
                            "2": "Half",
                            "variation_price": 6,
                            "3": 6,
                            "variation_saleprice": 5.5,
                            "4": 5.5,
                            "is_cart": 0
                        }`

I am able to decode the data from the api using: let parseData = try JSONDecoder().decode(CategoryViewModel2.self, from: ((response as? [String: Any])?.toData())!)

I am also able to print the response on my console as:

`parseData one is CategoryViewModel2(data: Optional([Paan_Casa.Datum2(the0: Optional(10), the1: Optional("Coffee"), the2: Optional("28-11-2022-1669614108unnamed.png"), the3: Optional(0), the4: Optional(1), id: Optional(10), name: Optional("Coffee"), image: Optional("28-11-2022-1669614108unnamed.png"), parentID: Optional(0), status: Optional(1), item: Optional([]), sub: nil), Paan_Casa.Datum2(the0: Optional(1), the1: Optional("Soups"), the2: Optional("29-09-2022-1664432271Homemade-Vegetable-Soup-Recipe-2-1200.jpeg"), the3: Optional(0), the4: Optional(1), id: Optional(1), name: Optional("Soups"), image: Optional("29-09-2022-1664432271Homemade-Vegetable-Soup-Recipe-2-1200.jpeg"), parentID: Optional(0), status: Optional(1), item: Optional([Paan_Casa.Item2(id: Optional(2), name: Optional("Subzi Ka Shorba"), image: nil, itemDescription: Optional("Borth of mixed veggies"), price: Optional(15), saleprice: Optional(12), parentID: Optional(1), isVariation: Optional(1), ftype: Optional(1), status: Optional(1), type: Optional("tm"), isCart: Optional(1), imageslider: Optional([Paan_Casa.Imageslider2(the0: Optional("29-09-2022-1664432939spring-vegetable-soup-7.jpeg"), imagename: Optional("29-09-2022-1664432939spring-vegetable-soup-7.jpeg"), imagepath: Optional("https://saidham.getfortune.app/administrator/business-admin/admin/uploads/category/29-09-2022-1664432939spring-vegetable-soup-7.jpeg"))]), isfavourite: Optional(0), variation: Optional([Paan_Casa.Variation2(the0: Optional(42), the1: Optional(2), the2: Optional("Half"), the3: Optional(6), the4: Optional(5), id: Optional(42), itemID: Optional(2), attributeValue: Optional("Half"), variationPrice: Optional(6), variationSaleprice: Optional(5), isCart: Optional(0)), Paan_Casa.Variation2(the0: Optional(41), the1: Optional(2), the2: Optional("Full"), the3: Optional(13), the4: Optional(11), id: Optional(41), itemID: Optional(2), attributeValue: Optional("Full"), variationPrice: Optional(13), variationSaleprice: Optional(11), isCart: Optional(1))])), Paan_Casa.Item2(id: Optional(1), name: Optional("Tamatar Ka Sorba"), image: nil, itemDescription: Optional("hj"), price: Optional(15), saleprice: Optional(11), parentID: Optional(1), isVariation: Optional(0), ftype: Optional(1), status: Optional(1), type: Optional("tm"), isCart: Optional(1), imageslider: Optional([Paan_Casa.Imageslider2(the0: Optional("29-09-2022-1664432864Tomato Soup.jpeg"), imagename: Optional("29-09-2022-1664432864Tomato Soup.jpeg"), imagepath: Optional("https://saidham.getfortune.app/administrator/business-admin/admin/uploads/category/29-09-2022-1664432864Tomato Soup.jpeg"))]), isfavourite: Optional(0), variation: nil)]), sub: nil)]))`

I wanna access the values stored in variation struct ie variation_saleprice I looked at questions similar to my problem but none of them had optionals in the struct. one of the answer by @ielyamani is Expected to decode Int but found a string the code

let houses = try JSONDecoder().decode(ServerStatus.self, from: data).data.houses

shall work to access the nested values inside of the house struct.

I tried to do it using let parseData = try JSONDecoder().decode(CategoryViewModel2.self, from: ((response as? [String: Any])?.toData())!).data.item but Xcode refuses to recognise the variable item.

0

There are 0 best solutions below