I am trying decode a JSON with below structure and convert to a Swift struct object.
"{\"createdAt\":\"2021-07-20T06:53:05.755Z\",\"extraPayload\":\"32bd7d6691964519\",\"messageID\":\"a5f77d0a27da2e14a78ce4d736590bb3c369d5e5bebd36339553e3b699b82d13\",\"publishedAt\":\"2021-07-20T06:53:05.836655Z\"}"
Both the publishedAt
and messageID
fields are fixed and any other fields in the JSON can be arbitrary based on a particular use-case.
In this case both fields createdAt
and extraPayload
are completely optional and another case it could be something like appConfig
:true
etc apart from the 2 fixed fields.
How can I map into a struct with below format which has 2 mandatory fixed fields and arbitrary part captured using a Dictionary of type [String:Any]
?
public struct MessagePayload {
/**
* Message identifier — opaque non-empty string.
*/
let messageID: String
/**
* ISO 8601 date indicating a moment when the message was published to the ACN back-end service.
*/
let publishedAt: String
/**
* Application-specific fields.
*/
let messageObject: [String:Any]?
}
I have tried using Swift Codable but it does not support [String:Any].