1

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
  • Are attribute1 & attribute2 always the same, or can the key be dynamic?
    – Larme
    Commented May 3, 2022 at 14:34
  • Dynamic. The attribute array can have N key/value pairs.
    – colbrew
    Commented May 3, 2022 at 14:43

2 Answers 2

2

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
0

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"}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.