title: Should I use an array or an object for REST API collections?
body: <p>My API contains a <code>Book</code> entity which represents a collection of several possible <code>BookContent</code> depending on the language. A <code>BookContent</code> is another entity with the attributes <code>Title</code> and <code>Content</code>, which both depend on the <code>Language</code>.</p>
  
  <p>Should a <code>Book</code> look like:</p>
  
  <p>1)</p>
  
  <pre><code>[
    {
       "language": "English",
       "title": "First title",
       "content": "First content"
    },
    {
       "language": "French",
       "title": "Premier titre",
       "content": "Premier contenu"
    }
  ]
  </code></pre>
  
  <p>or like:</p>
  
  <p>2)</p>
  
  <pre><code>{
    "English": {
       "title": "First title",
       "content": "First content"
    },
    "French": {
       "title": "Premier titre",
       "content": "Premier contenu"
    }
  }
  </code></pre>
  
  <p>The option 1):</p>
  
  <ul>
  <li>produces "self-contained" elements, i.e. each object contains all the information.</li>
  <li>The <code>language</code> attribute can contain any Unicode character (whereas as a key it cannot).</li>
  <li>is the only available option if the content depends on multiple criteria, i.e. language and year.</li>
  <li>creates a clearer separation between the two entities. For example, it makes it easier to replace each element by its ID, if we were to decide not to embed the <code>BookContent</code> entity anymore but only return the <code>BookContent</code> ID.</li>
  <li>is probably more familiar to developers using my API, since I believe it is more common to find this kind of structure in other REST APIs.</li>
  </ul>
  
  <p>The option 2):</p>
  
  <ul>
  <li>produces smaller elements.</li>
  <li>makes it faster to look for the elements according to the language without traversing through the whole collection.</li>
  </ul>
  
  <p>This is a general question to know which one looks more like a "best practice", with no assumption on how the clients are querying/using the REST API, nor how much performance matters as opposed to flexibility, etc.</p>
  
  <p>Which option is generally more often a "best practice"?</p>