Given the following JSON from a network request; If you wanted to decode this into a Swift object that coforms to Codable
, but you wanted to retain the nested JSON that is the value for the key configuration_payload
, how could you do it?
{
"registration": {
"id": "0000-0000-0000-0000-000",
"device_type": "device",
"state": "provisioning",
"thing_uuid": 999999999,
"discovery_timeout": 10,
"installation_timeout": 90,
"configuration_payload":
{
"title": "Some Title",
"url": "https://www.someurl.com/",
"category": "test",
"views": 9999
}
}
}
Using the following Swift struct
, I want to be able to grab the configuration_payload
as a String
.
public struct Registration: Codable {
public enum State: String, Codable {
case provisioning, provisioned
}
public let id, deviceType: String
public let state: State
public let error: String?
public let thingUUID: Int?
public let discoveryTimeout, installationTimeout: Int
public let configurationPayload: String?
}
As far as I can tell, the JSONDecoder
in Swift, sees the value for configuration_payload
as nested JSON and wants to decode it into it's own object. To add to confusion, configuration_payload
is not always going to return the same JSON structure, it will vary, so I can not create a Swift struct
that I can expect and simply JSON encode it again when needed. I need to be able to store the value as a String to account for variations in the JSON under the configuration_payload
key.