I am having trouble decoding my JSON. It says: "The data couldn’t be read because it isn’t in the correct format." I can't put my finger on it what's wrong. Would you mind to take a pick?
Endpoint: https://images-api.nasa.gov/search?q=apollo%2011&media_type=video
JSON Sample:
{ "collection": { "version": "1.0", "href": "https://images-api.nasa.gov/search?q=apollo%2011&media_type=video", "items": [ { "href": "https://images-assets.nasa.gov/video/Apollo 11 Overview/collection.json", "links": [ { "href": "https://images-assets.nasa.gov/video/Apollo 11 Overview/Apollo 11 Overview~thumb.jpg", "render": "image", "rel": "preview" }, { "href": "https://images-assets.nasa.gov/video/Apollo 11 Overview/Apollo 11 Overview.srt", "rel": "captions" } ], "data": [ { "description": "Video highlights from the historic first manned landing on the moon, during the Apollo 11 mission in July 1969.", "date_created": "2013-05-15T00:00:00Z", "media_type": "video", "keywords": [ "Apollo 11", "Moon" ], "nasa_id": "Apollo 11 Overview", "center": "HQ", "title": "Apollo 11 Overview" } ] }, My model:
struct NasaCollection: Codable { var collection: Collection } // MARK: - Collection struct Collection: Codable { let version: String let href: String let items: [Item] } // MARK: - Item struct Item: Codable { let href: String let links: [ItemLink] let data: Datum } // MARK: - ItemLink struct ItemLink: Codable { let href: String let render: Render? } // MARK: - Datum struct Datum: Codable { let datumDescription: String let dateCreated: Date let keywords: [String] let nasaID: String let title: String let location, description508, photographer, secondaryCreator: String? let album: [String]? } enum Render: String, Codable { case image = "image" } // MARK: - CollectionLink struct CollectionLink: Codable { let prompt, rel: String let href: String } // MARK: - Metadata struct Metadata: Codable { let totalHits: Int enum CodingKeys: String, CodingKey { case totalHits = "total_hits" } } Decding:
func getVideos(completed: @escaping (Result<[Item], Error>)-> Void) { let endpoint = "https://images-api.nasa.gov/search?q=apollo%2011&media_type=video" guard let url = URL(string: endpoint) else { return } let task = URLSession.shared.dataTask(with: url) { (data, response, error) in if let _ = error { completed(.failure(error?.localizedDescription as! Error)) } guard let response = response as? HTTPURLResponse, response.statusCode == 200 else { completed(.failure(error?.localizedDescription as! Error)) return } guard let data = data else { completed(.failure(error?.localizedDescription as! Error)) return } do { let decoder = JSONDecoder() decoder.dataDecodingStrategy = .base64 let videos = try decoder.decode([Item].self, from: data) completed(.success(videos)) } catch { print(error.localizedDescription) } } task.resume() } I think my model is correct and it "should" be decoded. I tried to decode the very root of JSON and still getting the same error.
print(error.localizedDescription). Justprint(error). You get a way better error message.