title: JSON API Error- The data couldn’t be read because it is missing.- in iOS, swift
body: <p>I have JSON data string structure like,</p>
  <pre><code>data: {
  &quot;items&quot;: [
      {
        &quot;kind&quot;: &quot;customsearch#result&quot;,
        &quot;title&quot;: &quot;XHAOYEAHX 40,60,110,150pcs Shoes Charms Fits ... - Amazon.com&quot;,
        &quot;snippet&quot;: &quot;Buy XHAOYEAHX 40, 60, 110, 150pcs Shoes Charms Fits for Croc Clog Shoes \nWristband Bracelet Party Girls Boys Gifts (Fixed Designs 100pcs) and other ...&quot;,
        &quot;pagemap&quot;: {
          &quot;scraped&quot;: [
            {
              &quot;image_link&quot;: &quot;https://images-na.ssl-images-amazon.com/images/I/91H%2BUO%2BMDQL._AC_UY395_.jpg&quot;
            }
          ],
          &quot;cse_image&quot;: [
            {
              &quot;src&quot;: &quot;https://images-na.ssl-images-amazon.com/images/I/91H%2BUO%2BMDQL._AC_UY395_.jpg&quot;
            }
          ]
        }
      }
  ]}
  </code></pre>
  <p>I tried to call the data model structure as ,</p>
  <pre><code> struct Items: Codable {
          var title:String?
          var snippet:String?
          var pagemap : PageMap?
          enum CodingKeys: String, CodingKey {
              case title = &quot;title&quot;
              case snippet = &quot;snippet&quot;
              case pagemap = &quot;pagemap&quot;
          }
          init(from decoder: Decoder) throws {
                 do {
                     let values = try decoder.container(keyedBy: CodingKeys.self)
                      title = try values.decode(String.self, forKey: .title)
                      snippet = try values.decode(String.self, forKey: .snippet)
                     pagemap = try values.decode(PageMap.self, forKey: .pagemap)
                    
                 } catch {
                     print(error.localizedDescription)
                 }
             }
      }
      struct PageMap: Codable {
          var cse_image: [Thumbnail]?
         private enum CodingKeys: String, CodingKey {
              case cse_image = &quot;cse_image&quot;
          }
          init(from decoder: Decoder) throws {
              do {
                  let values = try decoder.container(keyedBy: CodingKeys.self)
                  cse_image = try values.decode([Thumbnail].self, forKey: .cse_image)
                 
              } catch {
                  print(error.localizedDescription)
              }
          }
      }
      struct Thumbnail: Codable {
          let src: String?
          enum CodingKeys: String, CodingKey {
              case src = &quot;src&quot;
          }
          
      }
  </code></pre>
  <p>When I tried to call this structure and get the data from src string for image through dec.keyDecodingStrategy = .convertFromSnakeCase, shows error that data could'nt be read, because its missing, where did I went wrong ?</p>
  <pre><code>  let url = URL(string: &quot;https://www.googleapis.com/customsearch/v1?q=shoes&amp;key=myID&amp;cx=ursearchID&quot;)!
         let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
             if let error = error {
                 print(&quot;error: \(error)&quot;)
             } else {
                 if let response = response as? HTTPURLResponse {
                     print(&quot;statusCode: \(response.statusCode)&quot;)
                 }
              if let data = data, let dataString = String(data: data, encoding: .utf8) {
              print(&quot;data: \(dataString)&quot;)
                
                do{
                  let jsonStr = dataString
                   let dec = JSONDecoder()
                      dec.keyDecodingStrategy = .convertFromSnakeCase
                   let res = try dec.decode(Data.self, from: jsonStr.data(using:.utf8)!)
                
                  // self.items = [res]
                   for shop in res.items{
                      
                      self.shoptitle.append(shop.title!)
                      self.shopsnippet.append(shop.snippet!)
                       self.items.append(shop)
                      print(shop.snippet)
                      print(shop.pagemap?.cse_image)
                      if shop.pagemap?.cse_image != nil {
                          for info in (shop.pagemap?.cse_image!)! {
                          print(info.src)
                      }
                      }
                   }
                   
                   }
                   catch let error as NSError {
                       print(&quot;\(error)&quot;)
                   }
              }
             
                
             }
         }
         task.resume()
  </code></pre>