JSON-Decoder has an error - but I can't find out which!

Hello together,
I am developing some kind of "Product Overview"...

I receive this JSON from my API:

[
    {
        "id": 2,
        "name": "Coca Cola",
        "price": 2.2,
        "ust": 19,
        "options": [
                "small",
                "large"
        ]
    }
]

Then I am decoding with this Codable:

struct Product: Identifiable, Codable {
    var id: Int
    var name: String
    var price: Double
    var ust: Int
    var options: [String]?
}

And it throws an error when decoding:

AF.request("apiblablbablba").responseDecodable(of: [Product].self){response in
                    products = response.value ?? ([] as [Product])
                    if((response.error) != nil){
                        print(String(describing: response.error))
                    }
                }

And ALL THESE PARTS result in this error:

Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 5", intValue: 5), CodingKeys(stringValue: "options", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))))

I don't know why but CAN't find the problem! I already researched about the problem "Expected to decode Array but found a dictionary instead." - but they always said, the to the affected key corresponding value is a directory (so it is {bla}), but in my case it IS an array, isn't it?

I would be SO HAPPY if anybody could PLEASE help me with this!!!

Thanks and best greets,
Jannis

Is that your entire response there, or is there more than one item in the root array? I suggest you check your entire response to ensure options is always an array.

Hello, thanks for the quick response! Yes, there are multiple Products and it are ALWAYS arrays - here the full response - but it's in German:

[
    {
        "id": 2,
        "name": "Coca Cola klein",
        "price": 2.2,
        "ust": 19,
        "options": null
    },
    {
        "id": 3,
        "name": "Coca Cola groß",
        "price": 3.4,
        "ust": 19,
        "options": null
    },
    {
        "id": 4,
        "name": "1/4l Weißwein",
        "price": 5.8,
        "ust": 19,
        "options": null
    },
    {
        "id": 5,
        "name": "1/4l Rotwein",
        "price": 5.8,
        "ust": 19,
        "options": null
    },
    {
        "id": 8,
        "name": "Helles",
        "price": 2.9,
        "ust": 7,
        "options": [
            "klein",
            "gross"
        ]
    },
    {
        "id": 9,
        "name": "Pils",
        "price": 2.9,
        "ust": 7,
        "options": {
            "4": "klein",
            "5": "gross"
        }
    },
    {
        "id": 10,
        "name": "Milchreis",
        "price": 3.5,
        "ust": 7,
        "options": [
            "klein",
            "groß"
        ]
    },
    {
        "id": 11,
        "name": "Kuchen",
        "price": 3,
        "ust": 7,
        "options": [
            "mit Sahne"
        ]
    },
    {
        "id": 12,
        "name": "Potatoe Wedges m. Dip",
        "price": 3,
        "ust": 7,
        "options": [
            "mit Ketchup",
            "mit Mayo",
            "mit Salsa-Dip",
            "mit BBQ-Dip"
        ]
    }
]

OH DAMN - I just have seen the error - I think. I have to look up in the Symfony backend!

No, you've got a dictionary in there:

{
  "id": 9,
  "name": "Pils",
  "price": 2.9,
  "ust": 7,
  "options": {
    "4": "klein",
    "5": "gross"
  }
}

yes, I have just seen it... how can I close this thread? Or delete?

Can't delete now that there are responses, so don't worry about it.

Ok, thanks :)

Terms of Service

Privacy Policy

Cookie Policy