'Cannot infer contextual base in reference to member 'utf8' swift

I'm receiving the following error when trying to destructure an object:

Cannot infer contextual base in reference to member 'utf8'

Here is the object:

{
    ChallengeName = "CUSTOM_CHALLENGE";
    ChallengeParameters =     {
        USERNAME = "3bd8b0e1-37c2-4922-8c08-f2903d1a66d5";
        phone = "+13039318012";
    };
    Session = "AYABeLz0aQ7zXFus6wcPLxMd";
}

I get the above error when trying to destructure the object with this code:

let JSON = try JSONSerialization.jsonObject(with: data, options: [])
let jsonData = JSON.data(using: .utf8)! // this is where the error occurs

Perhaps there is something wrong with my struct?:

struct SessionData: Decodable {
    var ChallengeName: String
    var ChallengeParameters: String
    var Session: String
}

I



Solution 1:[1]

Solution:

request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
        
        let task = URLSession.shared.dataTask(with: request) {data, _, error in
            guard let data = data, error == nil else {
                return
            }
            
            do {
                let response = try JSONDecoder().decode(SessionData.self, from: data)
                print(response)
                
            } catch {
                print(error)
            }
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 moneyoceanswag