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

Accepted Reply

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,

  • Thank you for clarifying. I’ll keep an eye out for a fix in the upcoming betas.

    And yes, I’ll move that try! statement to my do block. Thanks!

  • Hello @JoeKun, decoding playlist now works as expected in iOS 15 beta 3. Thank you.

    However, now I am facing another issue. MusicPlayer isn't able to play a playlist. I am first setting setQueue(with:) to one of the decoded playlist and then trying to play it. Can you please help?

  • Hello @RahulRS , The problem you're facing now has the same root cause as the one described in this other thread. We are still investigating that other issue. Stay tuned. Best regards,

Replies

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,

  • Thank you for clarifying. I’ll keep an eye out for a fix in the upcoming betas.

    And yes, I’ll move that try! statement to my do block. Thanks!

  • Hello @JoeKun, decoding playlist now works as expected in iOS 15 beta 3. Thank you.

    However, now I am facing another issue. MusicPlayer isn't able to play a playlist. I am first setting setQueue(with:) to one of the decoded playlist and then trying to play it. Can you please help?

  • Hello @RahulRS , The problem you're facing now has the same root cause as the one described in this other thread. We are still investigating that other issue. Stay tuned. Best regards,