Search code examples
jsonswiftswift4decoder

JSON Decoder Swift 4.0


I have a JSON response from an API call.

I'm trying to use the Swift JSON Decoder however, I am getting the following error. [As per attached image] I'm assuming the JSON Decoder only works when you pull the JSON direct from a URL? - Correct me if I'm wrong. Is there a way to use the Swift JSONDecoder() on this API request with this result? Image attached with some JSON in my console.

Error, Request & JSON Result


Solution

  • The error says the decode method wants a Data, but you’re passing it a tuple containing an [Product] and a ProductListMeta?. In other words, you don’t have a JSON response. The JSON has already been decoded for you. Just use productList.products.