-1

I have a JSON object, I cannot figure out how to access the exclude_list in given JSON.

{"variants":
    {"variant_groups":
            [
                    {
                        "group_id":"1",
                        "name":"Crust",
                        "variations":[
                            {"name":"Thin","price":0,"default":1,"id":"1","inStock":1},
                            {"name":"Thick","price":0,"default":0,"id":"2","inStock":1,"isVeg":1},
                            {"name":"Cheese burst","price":100,"default":0,"id":"3","inStock":1,"isVeg":1}]},

                    {
                        "group_id":"3",
                        "name":"Sauce",
                        "variations":[
                            {"name":"Manchurian","price":20,"default":0,"id":"20","inStock":1,"isVeg":0},
                            {"name":"Tomato","price":20,"default":0,"id":"21","inStock":1,"isVeg":1},
                            {"name":"Mustard","price":20,"default":0,"id":"22","inStock":1,"isVeg":0}]

                    }],
            "exclude_list":[
                [
                    {"group_id":"1","variation_id":"3"},
                    {"group_id":"2","variation_id":"10"}
                ],
                [
                    {"group_id":"2","variation_id":"10"},
                    {"group_id":"3","variation_id":"22"}
                ]
            ]
    }
}

I can access properties like: variants, variant_groups using below Struct:

public struct VariantResponse: Codable {

    public let variants: Variants
}
public struct Variants: Codable {

    public let variantGroups:[VariantGroup]
    public let excludeList:[ExcludeItems]
}
public struct VariantGroup: Codable {
    public let groupId: String
    public let name: String
    public let variations: [Variant]
}
public struct Variant: Codable {
    public let name: String
    public let price: Int
    public let defaultValue: Int
    public let id: String
    public let inStock: Int
    public var isVeg: Int?

    private enum CodingKeys : String, CodingKey {
        case name, price, defaultValue = "default", id, inStock, isVeg
    }    
}
public struct ExcludeItems: Codable {
    public let excludes:[Exclude]
}
public struct Exclude: Codable {
    public let groupId: String
    public let variationId: String
}
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
Rocky
  • 2,903
  • 1
  • 22
  • 26

3 Answers3

1

You can try

struct VariantResponse: Codable {
    let variants: Variants
}

struct Variants: Codable {
    let variantGroups: [VariantGroup]
    let excludeList: [[ExcludeItems]]

}

struct ExcludeItems: Codable {
    let groupId, variationId: String

}

do {

    let dec = JSONDecoder()
    dec.keyDecodingStrategy = .convertFromSnakeCase
    let res = try dec.decode(VariantResponse.self, from: data)

}
catch {

    print(error)
}

ExcludeItems contains groupId, variationId but you add unknown key excludes

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

you have some problems in decodable structs , try to use this code :

    public struct VariantResponse: Codable {
    public let variants: Variants
}

public struct Variants: Codable {
    public let variant_groups:[VariantGroup]
    public let exclude_list:[[Exclude]]
}

public struct VariantGroup: Codable {
    public let group_id: String
    public let name: String
    public let variations: [Variant]
}

public struct Variant: Codable {
    public let name: String
    public let price: Int
    public let defaultValue: Int
    public let id: String
    public let inStock: Int
    public var isVeg: Int?

    private enum CodingKeys : String, CodingKey {
        case name, price, defaultValue = "default", id, inStock, isVeg
    }
}

public struct Exclude: Codable {
    public let group_id: String
    public let variation_id: String
}

Wishes best luck.

enter image description here

Abdulameer
  • 34
  • 5
0

The issue may be with the coding keys in your model. Please refer the following Model for fetching your JSON structure.

struct VariantsResponse: Codable {
    let variants: Variants?
}

typealias VariantGroups = [VariantGroup]
typealias ExcludeLists = [[ExcludeList]]
typealias Variations = [Variation]

struct Variants: Codable {
    let variantGroups: VariantGroups?
    let excludeList: ExcludeLists?

    enum CodingKeys: String, CodingKey {
        case variantGroups = "variant_groups"
        case excludeList = "exclude_list"
    }
}

struct ExcludeList: Codable {
    let groupId, variationId: String?

    enum CodingKeys: String, CodingKey {
        case groupId = "group_id"
        case variationId = "variation_id"
    }
}

struct VariantGroup: Codable {
    let groupId, name: String?
    let variations: Variations?

    enum CodingKeys: String, CodingKey {
        case groupId = "group_id"
        case name, variations
    }
}

struct Variation: Codable {
    let name: String?
    let price, variationDefault: Int?
    let id: String?
    let inStock, isVeg: Int?

    enum CodingKeys: String, CodingKey {
        case name, price
        case variationDefault = "default"
        case id, inStock, isVeg
    }
}

In the above model, I am creating typealias for the following arrays, for making the model more human readable and understandable.

typealias VariantGroups = [VariantGroup]
typealias ExcludeLists = [[ExcludeList]]
typealias Variations = [Variation]

here we are defining codingKeys case for variables which cannot convert directly to an attribute of the model.

Hope this will helps you to create the model.

You can simply decode the model with the responseData using the following code

let decoder = JSONDecoder()
let res = try decoder.decode(VariantsResponse.self, from: responseData)

Here if we specify the decoder.keyDecodingStrategy = .convertFromSnakeCase we didn't need to specify the codingKeys like case groupId = "group_id". The system will automatically inspect the _ character and convert the JSON key string to camelCaseKeys.

Febin Fathah
  • 417
  • 3
  • 9