title: How to deserialize JSON object of multiple key value entries to custom struct in Rust
body: <p>I am trying to deserialize a set of unknown key-value style labels from JSON into my struct.</p>
  <p>This is my current implementation of parsing the JSON:</p>
  <pre class="lang-rust prettyprint-override"><code>use std::collections::HashMap;
  use serde::{Serialize, Deserialize};
  use anyhow::Result;
  
  #[derive(Debug, Clone, Serialize, Deserialize)]
  pub struct Node {
      metadata: Metadata,
      pub spec: Spec,
  }
  
  #[derive(Debug, Clone, Serialize, Deserialize)]
  pub struct Metadata {
      name: String,
      labels: HashMap&lt;String, String&gt;,
      expires: String,
      id: i64,
  }
  
  #[derive(Debug, Clone, Serialize, Deserialize)]
  pub struct Spec {
      pub hostname: String,
  }
  
  fn main() -&gt; Result&lt;()&gt; {
      let json = r#&quot;
  [
    {
      &quot;metadata&quot;: {
        &quot;name&quot;: &quot;161bee39-cf07-4e31-90ba-6593c9f505cb&quot;,
        &quot;labels&quot;: {
          &quot;application&quot;: &quot;api&quot;,
          &quot;owner&quot;: &quot;team_x&quot;
        },
        &quot;expires&quot;: &quot;2021-12-06T20:49:04.136656523Z&quot;,
        &quot;id&quot;: 1638823144137190452
      },
      &quot;spec&quot;: {
        &quot;hostname&quot;: &quot;host1.example.com&quot;
      }
    },
    {
      &quot;metadata&quot;: {
        &quot;name&quot;: &quot;c1b3ee09-8e4a-49d4-93b8-95cbcb676f20&quot;,
        &quot;labels&quot;: {
          &quot;application&quot;: &quot;database&quot;,
          &quot;owner&quot;: &quot;team_y&quot;
        },
        &quot;expires&quot;: &quot;2021-12-06T20:49:55.23841272Z&quot;,
        &quot;id&quot;: 1638823195247684748
      },
      &quot;spec&quot;: {
        &quot;hostname&quot;: &quot;host2.example.com&quot;
      }
    }
  ]
      &quot;#;
      let nodes: Vec&lt;Node&gt; = serde_json::from_str(json)?;
      println!(&quot;{:?}&quot;, nodes);
      Ok(())
  }
  </code></pre>
  <p>The example works as it should, but now I would like to add a Label struct like this:</p>
  <pre class="lang-rust prettyprint-override"><code>#[derive(Debug, Clone, Serialize, Deserialize)]
  pub struct Metadata {
      name: String,
      labels: Vec&lt;Label&gt;,
      expires: String,
      id: i64,
  }
  
  #[derive(Debug, Clone, Serialize, Deserialize)]
  pub struct Label {
      key: String,
      value: String,
  }
  </code></pre>
  <p>This does obviously not work, but I am unsure how to go on from here. From my research prior to this question, I know that you can implement a custom Deserializer, but I could not find out how to properly do that. Maybe this is also not the best approach and I am not seeing the obvious solution.</p>
  <p>Thanks in advance for any example or help.</p>