title: Haskell :: Aeson :: parse ADT based on field value
body: <p>I'm using an external API which returns JSON responses. One of the responses is an array of objects and these objects are identified by the field value inside them. I'm having some trouble understanding how the parsing of such JSON response could be done with Aeson.</p>
  
  <p>Here is a simplified version of my problem:</p>
  
  <pre><code>newtype Content = Content { content :: [Media] } deriving (Generic)
  
  instance FromJSON Content
  
  data Media =
    Video { objectClass :: Text
          , title :: Text } |
    AudioBook { objectClass :: Text
              , title :: Text }
  </code></pre>
  
  <p>In API documentation it is said that the object can be identified by the field <em>objectClass</em> which has value "video" for our <em>Video</em> object and "audiobook" for our <em>AudioBook</em> and so on. Example JSON:</p>
  
  <pre><code>[{objectClass: "video", title: "Some title"}
  ,{objectClass: "audiobook", title: "Other title"}]
  </code></pre>
  
  <p>The question is how can this type of JSON be approached using Aeson?</p>
  
  <pre><code>instance FromJSON Media where
    parseJSON (Object x) = ???
  </code></pre>