0

I am getting many random issues. Mostly like some structure is not decodable not able to understand how to define structure.

Please find the code snipped

var 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":"2","name":"Size","variations":[{"name":"Small","price":0,"default":1,"id":"10","inStock":1,"isVeg":0},{"name":"Medium","price":100,"default":0,"id":"11","inStock":1,"isVeg":1},{"name":":Large","price":200,"default":0,"id":"12","inStock":1,"isVeg":0}]},{"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"}]]}}
""".data(using: .utf8)

/* 
 not sure is this the right way to define Root
*/
    struct Root : Codable {

        let variants : varientStruct
        let exclude_list : exclude_list

    }

    struct exclude_list : Codable{
        let variation_id : String
        let group_id : String
    }

    struct varientStruct: Codable {
        let variant_groups = [variant_groups_struct]
    }
    struct variant_groups_struct : Codable {
        let group_id : String
        let name :String
        let variations: [variationsStruct]
    }

    struct variationsStruct :Codable {
        let name : String
        let price : Int
        let selected: Int
        let id : String
        let inStock: Bool

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

do {
    let data = Data(person.utf8)
    let result = try JSONDecoder().decode(Root.self, from: JSON)
    print(result)
} catch  {
    print(error)
}
2
  • Your question is in the "here's a blob of code. Why does it have errors" category. What do you want from the JSON? Commented Dec 25, 2017 at 21:08
  • I m not able to understand how to parse variant_groups and exclude_list Commented Dec 25, 2017 at 21:32

1 Answer 1

6

First of all and once again, please conform to the naming convention:

  • struct and class names start with a uppercase letter.
  • Variable and function names start with a lowercase letter.
  • All variable and struct / class names are camelCased rather than snake_cased.

Second of all, JSON is very easy to read. There are only two collection types (array [] and dictionary {}) and four value types.

Format the JSON string to be able to recognize the structure more conveniently

let jsonString = """
{"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":"2","name":"Size","variations":
                                    [{"name":"Small","price":0,"default":1,"id":"10","inStock":1,"isVeg":0},
                                    {"name":"Medium","price":100,"default":0,"id":"11","inStock":1,"isVeg":1},
                                    {"name":":Large","price":200,"default":0,"id":"12","inStock":1,"isVeg":0}]
                                },{"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"}]]
            }
}
"""

Then build the structs according to the JSON structure step by step

struct Root : Decodable {
    let variants : Variant
}

struct Variant : Decodable {

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

    let groups : [VariantGroup]
    let excludeList : [[ExcludeList]]
}

struct VariantGroup : Decodable {

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

    let groupID : String
    let name : String
    let variations : [Variation]
}

struct Variation  : Decodable {

    let name : String
    let price : Int
    let `default` : Int
    let id : String
    let inStock : Int
}


struct ExcludeList : Decodable {
    private enum CodingKeys : String, CodingKey {
        case groupID = "group_id"
        case variationID = "variation_id"
    }

    let groupID : String
    let variationID : String
}

Then decode the stuff

do { 
   let data = Data(jsonString.utf8)      
   let result = try JSONDecoder().decode(Root.self, from: data)
   print(result)

} catch { print(error) }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help. I am really confused and not sure when to use Codabele/Encodable/Decodable ? I did not found any clear definition . please help me with rules .
Hi, How I can add concern groupId key and value in Variation structure ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.