-1

I'm trying to consume a JSON Rest API via Swifts Decodable class. The API contains a dictionary with keys as Strings and values that sometimes is a String and sometimes is Bool. I've created structs for all subtrees of the JSON, but can't figure out how to have a dictionary with values of string OR Bool.

It's the images-param (below) that is [String : String] OR [String : Bool].

struct Item:Decodable {
    var id: String
    var name: String
    var price: String
    var priceIcon: String
    var priceIconLink: String
    var images: [String : String]
    var rarity: String
    var type: String
    var readableType: String
}

Tried with this but it dosen't conform to Decodable.

var images: [String : Any]

Example of JSON response

"images": {
                "icon": "https://image.fnbr.co/emote/5ae8a0edf3d31bd9cac5b80d/icon.png",
                "png": "https://image.fnbr.co/emote/5ae8a0edf3d31bd9cac5b80d/png.png",
                "gallery": "https://image.fnbr.co/emote/5ae8a0edf3d31bd9cac5b80d/gallery.jpg",
                "featured": false
            }
picciano
  • 22,341
  • 9
  • 69
  • 82

1 Answers1

-1

SOLVED! I did some pre processing of the JSON-string before the parsing. Replaced all false with empty string ("").

Solution:

dataString.replacingOccurrences(of: ":false}", with: ":\"\"}", options: .literal, range: nil)