title: How to parse a JSON string using Aeson that can be one of two different types
body: <p>I'm currently struggling to parse some JSON data using the <code>aeson</code> library. There are a number of properties that have the value <code>false</code> when the data for that property is absent. So if the property's value is typically an array of integers and there happens to be no data for that property, instead of providing an empty array or <code>null</code>, the value is <code>false</code>. (The way that this data is structured isn't my doing so I'll have to work with it somehow.) </p>
  
  <p>Ideally, I would like to end up with an empty list in cases where the value is a boolean. I've created a small test case below for demonstration. Because my <code>Group</code> data constructor expects a list, it fails to parse when it encounters <code>false</code>.</p>
  
  <pre class="lang-hs prettyprint-override"><code>  data Group = Group [Int] deriving (Eq, Show)
  
    jsonData1 :: ByteString
    jsonData1 = [r|
      {
        "group" : [1, 2, 4]
      }
    |]
  
    jsonData2 :: ByteString
    jsonData2 = [r|
      {
        "group" : false
      }
    |]
  
    instance FromJSON Group where
      parseJSON = withObject "group" $ \g -&gt; do
        items &lt;- g .:? "group" .!= []
        return $ Group items
  
    test1 :: Either String Group
    test1 = eitherDecode jsonData1
    -- returns "Right (Group [1,2,4])"
  
    test2 :: Either String Group
    test2 = eitherDecode jsonData2
    -- returns "Left \"Error in $.group: expected [a], encountered Boolean\""
  </code></pre>
  
  <p>I was initially hoping that the <code>(.!=)</code> operator would allow it to default to an empty list but that only works if the property is absent altogether or <code>null</code>. If it were <code>"group": null</code>, it would parse successfully and I would get <code>Right (Group [])</code>.</p>
  
  <p>Any advice for how to get it to successfully parse and return an empty list in these cases where it's <code>false</code>?</p>