title: How do I convert JSON with variable variable names into swift object?
body: <p>I am working on a project with an API which gives me a JSON Object with some data in a variable. But the name of the variable under which the data is provided changes from day to day. For example today the name will be 285 and tomorrow it‘ll be 286. How can I convert this into a Swift object? I've already written some code so here it is:</p>
  <p>Getting data part:</p>
  <pre><code>func getData(){
          let semaphore = DispatchSemaphore.init(value: 0)
          
          let url = URL(string: URL_STRING)!
          var request = URLRequest(url: url)
          
          request.httpMethod = &quot;GET&quot;
          
          
          session.dataTask(with: request) { (dat, res, err) in
              
              print(&quot;Anfrage gestellt&quot;)
              
              guard let data = dat else {
                  print(&quot;no Data&quot;)
                  return
              }
              
              guard let resp = res else {
                  print(&quot;No Response &quot;)
                  return
              }
              
              if let error = err {
                  print(&quot;Error: \(error)&quot;)
                  return
              }
              
              
  
              do{
                  let decodedData = try self.decoder.decode(Report.self, from: data)
                  print(decodedData)
  
                  
              } catch {
                  print(&quot;Data decode failed \(error)&quot;)
              }
              
              semaphore.signal()
          }.resume()
          semaphore.wait()
          
          return
      }
  </code></pre>
  <p>The object in which it will be converted:</p>
  <pre><code>class Report: Codable{
      
      var keys: [String] = []
      var latest: String?
      let s: S
      
      init(keys: [String], s: S){
          self.keys = keys
          self.s = s
          latest = keys[keys.count - 1]
      }
      
      enum CodingKeys: String, CodingKey{
          case s = &quot;286&quot;
      }
      
  }
  </code></pre>
  <p>The JSON Object:</p>
  <pre><code>{
    &quot;285&quot;: {
      &quot;AT&quot;: {
        &quot;av&quot;: -72.1,
        &quot;ct&quot;: 113986,
        &quot;mn&quot;: -100.813,
        &quot;mx&quot;: -27.115
      },
      &quot;First_UTC&quot;: &quot;2019-09-15T01:13:15Z&quot;,
      &quot;HWS&quot;: {
        &quot;av&quot;: 4.347,
        &quot;ct&quot;: 54297,
        &quot;mn&quot;: 0.20600000000000002,
        &quot;mx&quot;: 21.272
      },
      &quot;Last_UTC&quot;: &quot;2019-09-16T01:52:49Z&quot;,
      &quot;PRE&quot;: {
        &quot;av&quot;: 742.003,
        &quot;ct&quot;: 89613,
        &quot;mn&quot;: 723.2129,
        &quot;mx&quot;: 757.8722
      },
      &quot;Season&quot;: &quot;spring&quot;,
      &quot;WD&quot;: {
        &quot;1&quot;: {
          &quot;compass_degrees&quot;: 22.5,
          &quot;compass_point&quot;: &quot;NNE&quot;,
          &quot;compass_right&quot;: 0.382683432365,
          &quot;compass_up&quot;: 0.923879532511,
          &quot;ct&quot;: 1
        },
        &quot;10&quot;: {
          &quot;compass_degrees&quot;: 225.0,
          &quot;compass_point&quot;: &quot;SW&quot;,
          &quot;compass_right&quot;: -0.707106781187,
          &quot;compass_up&quot;: -0.707106781187,
          &quot;ct&quot;: 6973
        },
        &quot;11&quot;: {
          &quot;compass_degrees&quot;: 247.5,
          &quot;compass_point&quot;: &quot;WSW&quot;,
          &quot;compass_right&quot;: -0.923879532511,
          &quot;compass_up&quot;: -0.382683432365,
          &quot;ct&quot;: 3196
        },
        &quot;12&quot;: {
          &quot;compass_degrees&quot;: 270.0,
          &quot;compass_point&quot;: &quot;W&quot;,
          &quot;compass_right&quot;: -1.0,
          &quot;compass_up&quot;: -0.0,
          &quot;ct&quot;: 3066
        },
        &quot;3&quot;: {
          &quot;compass_degrees&quot;: 67.5,
          &quot;compass_point&quot;: &quot;ENE&quot;,
          &quot;compass_right&quot;: 0.923879532511,
          &quot;compass_up&quot;: 0.382683432365,
          &quot;ct&quot;: 131
        },
        &quot;5&quot;: {
          &quot;compass_degrees&quot;: 112.5,
          &quot;compass_point&quot;: &quot;ESE&quot;,
          &quot;compass_right&quot;: 0.923879532511,
          &quot;compass_up&quot;: -0.382683432365,
          &quot;ct&quot;: 680
        },
        &quot;6&quot;: {
          &quot;compass_degrees&quot;: 135.0,
          &quot;compass_point&quot;: &quot;SE&quot;,
          &quot;compass_right&quot;: 0.707106781187,
          &quot;compass_up&quot;: -0.707106781187,
          &quot;ct&quot;: 9405
        },
        &quot;7&quot;: {
          &quot;compass_degrees&quot;: 157.5,
          &quot;compass_point&quot;: &quot;SSE&quot;,
          &quot;compass_right&quot;: 0.382683432365,
          &quot;compass_up&quot;: -0.923879532511,
          &quot;ct&quot;: 8813
        },
        &quot;8&quot;: {
          &quot;compass_degrees&quot;: 180.0,
          &quot;compass_point&quot;: &quot;S&quot;,
          &quot;compass_right&quot;: 0.0,
          &quot;compass_up&quot;: -1.0,
          &quot;ct&quot;: 8231
        },
        &quot;9&quot;: {
          &quot;compass_degrees&quot;: 202.5,
          &quot;compass_point&quot;: &quot;SSW&quot;,
          &quot;compass_right&quot;: -0.382683432365,
          &quot;compass_up&quot;: -0.923879532511,
          &quot;ct&quot;: 13801
        },
        &quot;most_common&quot;: {
          &quot;compass_degrees&quot;: 202.5,
          &quot;compass_point&quot;: &quot;SSW&quot;,
          &quot;compass_right&quot;: -0.382683432365,
          &quot;compass_up&quot;: -0.923879532511,
          &quot;ct&quot;: 13801
        }
      }
    },
      &quot;sol_keys&quot;: [
      &quot;285&quot;
    ]
  }
  </code></pre>
  <p>Thanks for your help</p>