title: Custom Decodable JSON - Get first array element
body: <p>I have JSON data that looks like this:</p>
  <pre><code>{
    // Other attributes here....
    &quot;id&quot;: &quot;a4s5d6f8ddw&quot;,
    &quot;images&quot;: {
      &quot;selection&quot;: [
        {
          &quot;url&quot;: &quot;https://www.myimage.com&quot;
        },
        // ... more images here
      ]
    }
  }
  </code></pre>
  <p>I want to extract the id and the first url from the nested JSON and store it in my <code>struct</code>. I've tried something like this, but I can't quite get it:</p>
  <pre><code>struct MyImage {
    let id: String
    let url: URL
  
    enum CodingKeys: CodingKey {
      case id
      case images
    }
  
    enum Selection: CodingKey {
      case selection
    }
  
    enum ImageURL: CodingKey {
       case url
    }
  }
  
  extension MyImage: Decodable {
    init(from decoder: Decoder) throws {
      let values = try decoder.container(keyedBy: CodingKeys.self)
      id = try values.decode(String.self, forKey: .id)
  
      let selection = try values.nestedContainer(keyedBy: Selection.self, forKey: .selection)
      // Then what?? Need to extract just the first one and then decode to a URL
  }
  </code></pre>
  <p>How can I finish this off?</p>