How do I convert a dictionary to top level JSON in Swift?

335 views Asked by At

I have this model in Swift:

struct EventModel: Codable {
    var eventType: String
    var eventName: String?
    var attributes: [String: String]?
}

Is it possible to move the attributes to the top level when I convert it to JSON? Example:

var model = EventModel(eventType: "type", 
                       eventName: "name", 
                       attributes: ["attribute1": "One", "attribute2": "Two"]) 

becomes

{
   "eventType" : "type",
   "eventName" : "name",
   "attribute1" : "One",
   "attribute2" : "Two"
}
2

There are 2 answers

0
Rob Napier On BEST ANSWER

First, encode the static keys, then encode the attributes into the same encoder:

extension EventModel: Encodable {
    enum CodingKeys: CodingKey {
        case eventType, eventName
    }

    func encode(to encoder: Encoder) throws {
        // Encode the normal stuff
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(eventType, forKey: .eventType)
        try container.encode(eventName, forKey: .eventName)

        // Then have the attributes dictionary encode itself
        try attributes?.encode(to: encoder)

    }
}
0
Joakim Danielson On

Convert the object to a dictionary using a computed property and encode the dictionary

extension EventModel {
    var asDictionary: [String: String] {
        var dictionary = attributes ?? [:]
        dictionary["evenType"] = eventType
        if let eventName = eventName { dictionary["eventName"] = eventName }
        return dictionary
    }
}

do {
    let data = try JSONEncoder().encode(model.asDictionary)
    if let s = String(data: data, encoding: .utf8) { print(s)}
} catch {
    print(error)
}

{"attribute1":"One","evenType":"type","eventName":"name","attribute2":"Two"}