Lets say I have the following struct and json data. Can We write a init(decode: Decoder) function to covert object with this condition? If the randomKey for the OptionContainer exist, use the randomKey value to look for the id for the option. If the randomKey doesn’t exist, then simply use the id.
Example, both the ids for the two options below would be ["1","2","3"]
My Struct
struct OptionContainer: Decodable {
let randomKey: String?
let id: String
let text: String
struct Option: Decodable {
let id: String
let text: String
}
}
Json Data
[
{
"id": "123",
"text": "text",
"randomKey": "level",
"options": [
{
"text": "Hello",
"level": "1"
},
{
"text": "Hello2",
"level": "2"
},
{
"text": "Hello3",
"level": "3"
},
]
},
{
"id": "222",
"text": "text2",
"options": [
{
"text": "Hello",
"id": "1"
},
{
"text": "Hello2",
"id": "2"
},
{
"text": "Hello3",
"id": "3"
},
]
},
]




