title: Why is my decoder not able to decode my api call with Alamofire in swift
body: <p>So I'm trying to to process my API call data into an object I'm calling PlaceModel.</p>
  <p>The API returns a lot of data; but I only need name, and coordinate data for my purposes at the moment.</p>
  <p>example of API data</p>
  <pre><code>--- redacted for brevity ---
  {
     &quot;html_attributions&quot; : [],
     &quot;next_page_token&quot; : &quot;AeJbb3dLZZ4oYIangr8wO9j6SE4_5L1NcYQC-qCKt1Y0saPFOzNc90E7bG8IDtj8DNlEeM6nWlClZgFd-YMtB4kGQYBLexZgjlX-nH9rN4UQHk9hk-Ha9zLpjY0GYiUsayMZKW5x7oAkQOXlmbARFxu2tR-dW5VB_dd0VkJkf7_6zeGeeje0UjQeqdk1Czr-gBgNxzmMj0adrj2_G0CxeJxk5eN645sdbZ8QJ-BChqkUegJ8l9V7dO4nRCfPz5u_paqoCEcVOJqYMyiZsc3ibBxNowY-uH2PCbpyXcM186CenqBIG088TiyulBFL4Dq1zDxXbZ5asSH4r-u8UtWpdpEcpzAJMjIT4W64vvXYZUT3_918VgENIyCEkPQVebjZUF0X2USykC-ZdlsB2BMvxjKlwx5Q_7_yKYy3Gnxq6T6jTgGisAvNXQ&quot;,
     &quot;results&quot; : [
        {
           &quot;business_status&quot; : &quot;OPERATIONAL&quot;,     // FIRST BUSINESS
           &quot;geometry&quot; : {
              &quot;location&quot; : {
                 &quot;lat&quot; : 49.24665,
                 &quot;lng&quot; : -122.7504486    // HERE IS SOME LOCATION INFO
              },
              &quot;viewport&quot; : {
                 &quot;northeast&quot; : {
                    &quot;lat&quot; : 49.24808953029149,
                    &quot;lng&quot; : -122.7490256697085
                 },
                 &quot;southwest&quot; : {
                    &quot;lat&quot; : 49.2453915697085,
                    &quot;lng&quot; : -122.7517236302915
                 }
              }
           },
           &quot;icon&quot; : &quot;https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/school-71.png&quot;,
           &quot;icon_background_color&quot; : &quot;#7B9EB0&quot;,
           &quot;icon_mask_base_uri&quot; : &quot;https://maps.gstatic.com/mapfiles/place_api/icons/v2/school_pinlet&quot;,
           &quot;name&quot; : &quot;Ascension Martial Arts&quot;,   // HERE IS A NAME
           &quot;photos&quot; : [
              {
                 &quot;height&quot; : 1668,
                 &quot;html_attributions&quot; : [
                    &quot;\u003ca href=\&quot;https://maps.google.com/maps/contrib/115539551484221381565\&quot;\u003eAscension Martial Arts\u003c/a\u003e&quot;
                 ],
                 &quot;photo_reference&quot; : &quot;AeJbb3dunz49PLKAwwhcbLid5d2yWHftgLOY5sdMPLXhCKohiOcfvinKkmFxIht8ZBSNgxTFVJCCbnHxOG7ROlt093iHntMlX4O80ihQ0MI80u7u_TvvKMcwu3os0tgfr84KaNVP1KzNrzTiS1GUF0S82OM43NEuWoMjBW5o2PmhH-Ke-tj4&quot;,
                 &quot;width&quot; : 2500
              }
           ],
           &quot;place_id&quot; : &quot;ChIJST1805B2hlQRASo6CA1ejKw&quot;,
           &quot;plus_code&quot; : {
              &quot;compound_code&quot; : &quot;66WX+MR Port Coquitlam, BC, Canada&quot;,
              &quot;global_code&quot; : &quot;84XV66WX+MR&quot;
           },
           &quot;rating&quot; : 4.9,
           &quot;reference&quot; : &quot;ChIJST1805B2hlQRASo6CA1ejKw&quot;,
           &quot;scope&quot; : &quot;GOOGLE&quot;,
           &quot;types&quot; : [ &quot;gym&quot;, &quot;health&quot;, &quot;point_of_interest&quot;, &quot;establishment&quot; ],
           &quot;user_ratings_total&quot; : 107,
           &quot;vicinity&quot; : &quot;109-1320 Kingsway Avenue, Port Coquitlam&quot;
        },
        {
           &quot;business_status&quot; : &quot;OPERATIONAL&quot;,      // NEXT BUSINESS
  
  --- redacted for brevity ---
  </code></pre>
  <p>So I really only need those 2 pieces of data for marking spots on my map.</p>
  <p>Now I'm just trying to debug it and get it to work making Struct objects but I'm stuck.</p>
  <p>networkingLayerAF.swift</p>
  <pre><code>import Foundation
  import Alamofire
  
  class NetworkingLayerAF{
      
      func getPlaces() {
                  
          let longitudeX = &quot;-122.735960&quot;
          let latitudeY = &quot;49.252400&quot;
          
          let url = URL(string:&quot;https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(latitudeY),\(longitudeX)&amp;radius=150000&amp;types=gym&amp;key=NFS&quot;)!
       
  //        AF.request(url).response{ response in
  //            debugPrint(response)
  //        }
          
          print(&quot;LAUNCH ALAMO&quot;)
          AF.request(url).responseDecodable(of: [PlaceModel].self){ response in
              switch response.result {
              case .success(let items):
                  print(items)
                  
              case .failure(let error):
                  print(error.localizedDescription)
              }
          }  
      }//
  }// end class
  </code></pre>
  <p>Error I'm getting when it runs in console.</p>
  <pre><code>LAUNCH ALAMO
  2022-07-31 17:06:24.217509-0700 MyAPP[14661:1355709] [boringssl] boringssl_metrics_log_metric_block_invoke(153) Failed to log metrics
  Response could not be decoded because of error:
  The data couldn’t be read because it isn’t in the correct format.
  </code></pre>
  <p>PlaceModel</p>
  <pre><code>import Foundation
  
  struct PlaceModel: Decodable {
      var name: String
      var coordinate: Coordinate
      
      enum CodingKeys: String, CodingKey {
          case name = &quot;name&quot;
          case coordinate
      }
  }
  
  struct Coordinate {
      var latitude: Double
      var longitude: Double
  
      enum CodingKeys: String, CodingKey {
          case latitude
          case longitude
      }
  }
  
  extension Coordinate: Decodable {
      public init(from decoder: Decoder) throws {
          let values = try decoder.container(keyedBy: Coordinate.CodingKeys.self)
          latitude = try values.decode(Double.self, forKey: .latitude)
          longitude = try values.decode(Double.self, forKey: .longitude)
      }
  }
  </code></pre>
  <p>Earlier I was trying to use this websites json to swift converter which gave me a different looking struct class</p>
  <p>placeModel - I added quite a few decodable tags onto the converter's code.</p>
  <pre><code>import Foundation
  import CoreLocation
  
  struct Welcome1: Decodable{
          let nextPageToken: String
          let results: [Result]
          let status: String
      }
  
      // MARK: - Result
  struct Result : Decodable{
          let businessStatus: BusinessStatus
          let geometry: Geometry
          let icon: String
          let iconBackgroundColor: IconBackgroundColor
          let iconMaskBaseURI: String
          let name: String
          let photos: [Photo]?
          let placeID: String
          let plusCode: PlusCode
          let rating: Double?
          let reference: String
          let scope: Scope
          let types: [TypeElement]
          let userRatingsTotal: Int?
          let vicinity: String
          let openingHours: OpeningHours?
      }
  
  enum BusinessStatus: CodingKey , Decodable{
          case operational
      }
  
      // MARK: - Geometry
  struct Geometry : Decodable {
          let location: Location
          let viewport: Viewport
      }
  
      // MARK: - Location
  struct Location : Decodable{
          let lat, lng: Double
      }
  
      // MARK: - Viewport
  struct Viewport : Decodable{
          let northeast, southwest: Location
      }
  
      enum IconBackgroundColor: CodingKey, Decodable {
          case the7B9Eb0
      }
  
      // MARK: - OpeningHours
  struct OpeningHours : Decodable{
          let openNow: Bool
      }
  
      // MARK: - Photo
  struct Photo : Decodable{
          let height: Int
          let htmlAttributions: [String]
          let photoReference: String
          let width: Int
      }
  
      // MARK: - PlusCode
  struct PlusCode : Decodable{
          let compoundCode, globalCode: String
      }
  
      enum Scope: CodingKey , Decodable{
          case google
      }
  
      enum TypeElement: CodingKey , Decodable {
          case establishment
          case gym
          case health
          case pointOfInterest
          case school
      }
  
  
  </code></pre>
  <p>But I kept getting pretty much the same error</p>
  <pre><code>LAUNCH ALAMO
  Response could not be decoded because of error:
  The data couldn’t be read because it isn’t in the correct format.
  </code></pre>
  <p>One thing I will note; when I use print(error) instead of LocalizedDescription I get this error:</p>
  <pre><code>LAUNCH ALAMO
  2022-07-31 17:33:09.489244-0700 Body Fat Calculator[15033:1371997] [boringssl] boringssl_metrics_log_metric_block_invoke(153) Failed to log metrics
  responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.typeMismatch(Swift.Array&lt;Any&gt;, Swift.DecodingError.Context(codingPath: [], debugDescription: &quot;Expected to decode Array&lt;Any&gt; but found a dictionary instead.&quot;, underlyingError: nil))))
  </code></pre>
  <p>So I edited the code a little to:
  AF.request(url).responseDecodable(of: <em><strong>Welcome1</strong></em>.self) // Not [Welcome1] now.</p>
  <p>and I'm getting some new errors</p>
  <p>Welcome1</p>
  <pre><code>LAUNCH ALAMO
  responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: &quot;nextPageToken&quot;, intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: &quot;No value associated with key CodingKeys(stringValue: \&quot;nextPageToken\&quot;, intValue: nil) (\&quot;nextPageToken\&quot;).&quot;, underlyingError: nil))))
  </code></pre>
  <p>PlaceModel</p>
  <pre><code>LAUNCH ALAMO
  2022-07-31 17:43:32.518882-0700 Body Fat Calculator[15209:1379339] [boringssl] boringssl_metrics_log_metric_block_invoke(153) Failed to log metrics
  responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: &quot;name&quot;, intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: &quot;No value associated with key CodingKeys(stringValue: \&quot;name\&quot;, intValue: nil) (\&quot;name\&quot;).&quot;, underlyingError: nil))))
  
  </code></pre>
  <p>Ok this getting a bit long, I'm going to keep trying stuff any insights appreciated happy to provide more info if needed :)</p>
  <p>Thank you :D</p>