I am trying to decode a JSON that one of the keys changes:
[
{
"id": "device:us-east-1",
"serial": "eng031",
"account": "9340370e",
"name": "moti123",
"tags": {
"group": [
"office"
]
},
"isAttached": true,
"isShared": false
},
{
"id": "lambda:device:us-east-1",
"serial": "106",
"account": "9340370e",
"name": "roei106 ",
"tags": {
"comment": [
"Iron Maiden "
]
},
"isAttached": true,
"isShared": false
}
]
As you can see the tags has a nested JSON with different keys in these example: group and comment.
What I did before is pretty simple. I had these structs:
public struct Device: Decodable {
public init() {}
public var id: String = ""
public var serial: String = ""
public var account: String = ""
public var name: String = ""
public var isAttached: Bool?
public var isShared: Bool?
public var tags: Tags?
}
public struct Tags: Decodable {
public init() {}
public var comment: [String] = [""]
}
And I used to parse them as follow:
func getDevices(data: Data) -> [Device] {
var devices = [Device]()
do {
let parsedModel = try JSONDecoder().decode([Device].self, from: data)
devices = parsedModel
print(parsedModel)
} catch let error{
print(error)
}
return devices
}
It all have worked fine until the key in the tags changed. Is there any way to know what is the key and change it inside the tag struct?