Cannot decode library playlists response

Hello,

I am fetching library playlists using the endpoint: https://api.music.apple.com/v1/me/library/playlists.

After that, I am trying to decode it using a custom struct that has a property named data which is array of Playlist.

Here is my code:

struct MyPlaylistsResponse: Decodable {
	let data: [Playlist]
}
let url = URL(string: "https://api.music.apple.com/v1/me/library/playlists")!
let dataRequest = MusicDataRequest(urlRequest: URLRequest(url: url))
let dataResponse = try! await dataRequest.response()
let decoder = JSONDecoder()
do {
    let playlistsResponse = try decoder.decode(MyPlaylistsResponse.self, from: dataResponse.data)
} catch let error {
    print("**** ERROR ****")
    print(error)
}

But the decoding isn’t working. It is giving me the following error:

valueNotFound(Swift.Int, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "attributes", intValue: nil), CodingKeys(stringValue: "artwork", intValue: nil), CodingKeys(stringValue: "width", intValue: nil)], debugDescription: "Expected Int value but found null instead.", underlyingError: nil))

The response is a playlist and I am trying to decode it using a custom Decodable struct that has an array of Playlist. Decoding Genres work, as shown in session, but playlists aren't working. Am I doing something wrong?

Thank you

Answered by Frameworks Engineer in 679556022

Hello @RahulRS,

What you’re trying to do here is meant to be supported. The error you pasted is enough for us to know how to address this issue. We’ll work on a fix.

However, you should definitely not use try! for fetching the response. I recommend you move that line to your do block, so you can just use try.

Thank you very much for your valuable feedback.

Best regards,

Accepted Answer

Hello @RahulRS,

What you’re trying to do here is meant to be supported. The error you pasted is enough for us to know how to address this issue. We’ll work on a fix.

However, you should definitely not use try! for fetching the response. I recommend you move that line to your do block, so you can just use try.

Thank you very much for your valuable feedback.

Best regards,

Cannot decode library playlists response
 
 
Q