title: How to parse dynamic JSON keys in dart
body: <p>I have an API that returns JSON with dynamic keys, i.e. the key value changes on every <code>GET</code> request.</p>
  <p>Ex: This is the format of the JSON.</p>
  <p>I have a book model in dart,</p>
  <pre class="lang-dart prettyprint-override"><code>class Book {
    String id = &quot;&quot;;
    String title = &quot;&quot;;
    
    Book();
    
    Book.fromJson(Map&lt;String, dynamic&gt; json) {
      id = json['id'];
      title = json['title'];
    }
    
    static List&lt;Book&gt; listFromJson(List&lt;dynamic&gt; json) {
      return json.map((val) =&gt; Book.fromJson(val)).toList();
    }
  }
  </code></pre>
  <p>and JSON response,</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;Recommended For You&quot; : [
          {
              &quot;id&quot; : &quot;001&quot;,
              &quot;title&quot; : &quot;title001&quot;
          },
          ...
      ],
      &quot;Thrillers&quot; : [
          {
              &quot;id&quot; : &quot;005&quot;,
              &quot;title&quot; : &quot;title005&quot;
          },
          ...
      ]
  }
  </code></pre>
  <p>How to parse this into lists of books according to the genre, given that genres (like <code>Thrillers</code>) can change(i.e. it is dynamic and may change for each user)?</p>
  <h1>Edit 1</h1>
  <p><code>Thrillers</code> and <code>Recommended For You</code> aren't constant, some times it may change to <code>Horror</code> and <code>Romantic</code> or something else. It is <strong>dynamic</strong></p>
  <h1>Edit 2 : The solution</h1>
  <p>Thanks to @lrn's solution, I came up with a new Class</p>
  <pre class="lang-dart prettyprint-override"><code>class Recommendations {
    String recommendationType;
    List&lt;Book&gt; recommendations;
  
    @override
    String toString() {
      return 'Recomendations[title: $recommendationType]';
    }
  
    Recommendations.fromJson(MapEntry&lt;String, dynamic&gt; json) {
      if (json == null) return;
      this.recommendationType = json.key;
      this.recommendations = Book.listFromJson(json.value);
    }
  
    static List&lt;Recommendations&gt; listFromJson(Map&lt;String, dynamic&gt; json) {
      return json == null
          ? List&lt;Recommendations&gt;.empty()
          : json.entries
              .map((value) =&gt; new Recommendations.fromJson(value))
              .toList();
    }
  }
  </code></pre>