title: Decode JSON object to plain String in Swift
body: <h2>Context</h2>
  <p><em><strong>This is a follow-up question to <a href="https://stackoverflow.com/questions/64636031/swift-json-decoding-with-dependent-types">that question</a> I asked a few days ago, reading it beforehand is not strictly necessary though.</strong></em></p>
  <p>I have an API endpoint <code>/common</code>, returning JSON data in that form:</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;data&quot;:
      {
          &quot;players&quot;: [
          {
              &quot;id&quot;: 1,
              &quot;name&quot;: &quot;John Doe&quot;
          },
          {
              &quot;id&quot;: 15,
              &quot;name&quot;: &quot;Jessica Thump&quot;
          }],
          &quot;games&quot;: [
          {
              &quot;name&quot;: &quot;Tic Tac Toe&quot;,
              &quot;playerId1&quot;: 15,
              &quot;playerId2&quot;: 1
          }]
      }
  }
  </code></pre>
  <p><em>In further code snippets, it is assumed that this response is stored as a <code>String</code> in the variable <code>rawApiResponse</code>.</em></p>
  <p>My aim is to decode that to according Swift <code>struct</code>s:</p>
  <pre class="lang-swift prettyprint-override"><code>struct Player: Decodable {
      var id: Int
      var name: String?
  }
  
  struct Game: Decodable {
      var name: String
      var player1: Player
      var player2: Player
      
      enum CodingKeys: String, CodingKey {
          case name
          case player1 = &quot;playerId1&quot;
          case player2 = &quot;playerId2&quot;
      }
  }
  </code></pre>
  <p>Thanks to the answer in my original question, I can now decode <code>Player</code>s and <code>Game</code>s successfully, but only when the response <code>String</code> I use is the inner array, <code>e.g.</code>:</p>
  <pre class="lang-swift prettyprint-override"><code>let playersResponse = &quot;&quot;&quot;
  [
      {
          &quot;id&quot;: 1,
          &quot;name&quot;: &quot;John Doe&quot;
      },
      {
          &quot;id&quot;: 15,
          &quot;name&quot;: &quot;Jessica Thump&quot;
      }
  ]
  &quot;&quot;&quot;
  
  let players = try! JSONDecoder().decode([Player].self, from: playersResponse.data(using: .utf8)!)
  </code></pre>
  <h2>The question</h2>
  <p><strong>How can I extract only the JSON <code>&quot;players&quot;</code> array from <code>/common</code>'s API response, so that I can feed it afterwards to a JSON decoder for my <code>Player</code>s?</strong></p>
  <p>Please note that I can't use (or that's at least what I think) the &quot;usual&quot; <code>Decodable</code> way of making a super-<code>Struct</code> because I need <code>players</code> to be decoded before <code>games</code> (that was the topic of the original question). So, this doesn't work:</p>
  <pre class="lang-swift prettyprint-override"><code>struct ApiResponse: Decodable {
      let data: ApiData
  }
  
  struct ApiData: Decodable {
      let players: [Player]
      let games: [Game]
  }
  let data = try! JSONDecoder().decode(ApiResponse.self, from: rawApiResponse.data(using: .utf8)!)
  </code></pre>
  <h2>What I tried so far</h2>
  <p>I looked into <a href="https://stackoverflow.com/questions/30480672/how-to-convert-a-json-string-to-a-dictionary">how to convert a JSON string to a dictionary</a> but that only partially helped:</p>
  <pre class="lang-swift prettyprint-override"><code>let json = try JSONSerialization.jsonObject(with: rawApiResponse.data(using: .utf8)!, options: .mutableContainers) as? [String:AnyObject]
  let playersRaw = json![&quot;data&quot;]![&quot;players&quot;]!!
  </code></pre>
  <p>If I dump <code>playersRaw</code>, it looks like what I want, but I have no clue how to cast it to <code>Data</code> to pass it to my <code>JSONDecoder</code>, as <code>type(of: playersRaw)</code> is <code>__NSArrayM</code>.</p>
  <hr />
  <p>I feel like I'm not doing things the way they should be done, so <strong>if you have a more &quot;Swifty&quot; solution to the general problem</strong> (and not specifically to how to extract a subset of the JSON data), it would be even nicer!</p>