NIJ MEHAR
NIJ MEHAR

Reputation: 9

not able to decode json in swift

This is the http client and the response is correct when printing the string, but it is showing conversion error when trying to decode

 let task = URLSession.shared.dataTask(with: request){ (data, response, error) in
                
                // Check for Error
                if let error = error {
                    print("Error took place \(error)")
                    return
                }
            do{
                let imageData = try JSONDecoder().decode(ImageDataModel.self,
                                                         from: data!)
                print(imageData)
            }catch{
                print("conversion error")
            }
  
            
        }

This is the data model used to decode


struct ImageDataModel: Decodable,Identifiable {
    var id = UUID()
    var created: Int
    var data:Array<ImageData>

    struct ImageData: Decodable,Identifiable {
        var id = UUID()
        var url: String
    }

    init(created: Int, data: [ImageData]) {
        self.created = created
        self.data = data
    }
}

The response is

{
    "created": 1670598580,
    "data": [
        {
            "url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-t0OU052mWtWLkmxpWHfSoME9/user-mgq3wJsAIptbbj1l0yI0YIS4/img-WafK921SOTYJSY0oub4e0z4f.png?st=2022-12-09T14%3A09%3A40Z&se=2022-12-09T16%3A09%3A40Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2022-12-09T01%3A07%3A28Z&ske=2022-12-10T01%3A07%3A28Z&sks=b&skv=2021-08-06&sig=McfqQuL5FZ%2B0Ow1qms3i3phdYMZjmxcoNcfr4/7/XPk%3D"
        },
        {
            "url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-t0OU052mWtWLkmxpWHfSoME9/user-mgq3wJsAIptbbj1l0yI0YIS4/img-FgLNjPkxOcKk0wOz5pGKYO5c.png?st=2022-12-09T14%3A09%3A40Z&se=2022-12-09T16%3A09%3A40Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2022-12-09T01%3A07%3A28Z&ske=2022-12-10T01%3A07%3A28Z&sks=b&skv=2021-08-06&sig=h72lRrBUed0HKHkS3QEHy2RzfM65xbwtlJMM1NGK9w0%3D"
        },
        {
            "url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-t0OU052mWtWLkmxpWHfSoME9/user-mgq3wJsAIptbbj1l0yI0YIS4/img-mV5jtC9UQatb3zoDVG8lQw4u.png?st=2022-12-09T14%3A09%3A40Z&se=2022-12-09T16%3A09%3A40Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2022-12-09T01%3A07%3A28Z&ske=2022-12-10T01%3A07%3A28Z&sks=b&skv=2021-08-06&sig=pYrewa9h/B0gzOp7NwfzYwjdVlG4%2BwkpJCXdzuEw9HY%3D"
        },
        {
            "url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-t0OU052mWtWLkmxpWHfSoME9/user-mgq3wJsAIptbbj1l0yI0YIS4/img-PjwW7ctrLEjFkIuctE71V2jj.png?st=2022-12-09T14%3A09%3A40Z&se=2022-12-09T16%3A09%3A40Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2022-12-09T01%3A07%3A28Z&ske=2022-12-10T01%3A07%3A28Z&sks=b&skv=2021-08-06&sig=Mznigp33qXj1f2yO1kUfxiwJOG2XiAfKpXdBQBavu8Q%3D"
        },
        {
            "url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-t0OU052mWtWLkmxpWHfSoME9/user-mgq3wJsAIptbbj1l0yI0YIS4/img-3vZsW3Tr82PELAH2QDnGz2im.png?st=2022-12-09T14%3A09%3A40Z&se=2022-12-09T16%3A09%3A40Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2022-12-09T01%3A07%3A28Z&ske=2022-12-10T01%3A07%3A28Z&sks=b&skv=2021-08-06&sig=cIwENFt/fWP12wLtr%2BNQks6OuCgB%2BZORsT6dxHbk7ys%3D"
        }
    ]
}

I was expecting to store the json received into the struct

Upvotes: 0

Views: 98

Answers (1)

vadian
vadian

Reputation: 285079

The error is clear:

No value associated with key CodingKeys(stringValue: "id", intValue: nil)

You declared an id property but there is no key id in the JSON.

Basically there are two options:

  1. Declare id as constant. It makes no sense anyway to declare an unique identifier mutable

    let id = UUID()
    

    You will get a warning but it can be ignored.

  2. Specify explicit CodingKeys omitting id

    struct ImageDataModel: Decodable, Identifiable {
        private enum CodingKeys: String, CodingKey { case created, data}
    
        let id = UUID()
        var created: Int
        var data: Array<ImageData>
    
        struct ImageData: Decodable, Identifiable {
            private enum CodingKeys: String, CodingKey { case url }
    
        ...
    

Side note: Never print just a meaningless literal string in a catch block. Print always at least the error instance

 } catch {
     print("conversion error", error)
 }

Upvotes: 1

Related Questions