title: Omitting Nothing/null fields in Haskell's Aeson
body: <p>I have a type</p>
  
  <pre><code>{-# LANGUAGE DeriveGeneric   #-}
  {-# LANGUAGE TemplateHaskell #-}
  {-# LANGUAGE MultiWayIf      #-}
  
  import GHC.Generics
  import Data.Aeson.TH
  import Data.Aeson.Types
  
  data MyJSONObject = MyJSONObject
    { name    :: String
    , ptype   :: Maybe String
    , pid     :: Maybe String
    , subject :: Maybe String
    , message :: Maybe String
    } deriving (Show, Generic)
  </code></pre>
  
  <p>with a great many more <code>Maybe String</code> fields.  My <code>FromJSON</code> and <code>ToJSON</code> are provided by the <code>TemplateHaskell</code> function</p>
  
  <pre><code>$(deriveJSON defaultOptions
    {
      omitNothingFields  = True
    , fieldLabelModifier = \f -&gt; if
        | f == "ptype" -&gt; "type" -- reserved keyword
        | f == "pid"   -&gt; "id"   -- Prelude function name
        | otherwise    -&gt; f
    } ''MyJSONObject)
  </code></pre>
  
  <p>Ultimately, the output of the program is a JSON document intended to be consumed by an application that does not permit some fields to have null values, even if it does allow these fields to not exist.  In other words, it's perfectly fine for <code>subject</code> to not be present in the JSON document, but if it does exist, its value cannot be null.  My expectation was that <code>omitNothingFields</code> would handle this requirement, but this does not appear to be the case: the <code>decode</code>d JSON still has <code>Nothing</code> values for fields that are not present, and the <code>encode</code>d JSON has <code>null</code> values for those fields.  The former case is fine; the latter case is not, hence the question.   </p>
  
  <p>Am I misusing, or misunderstanding the purpose of, <code>omitNothingFields</code>?  How can I ignore fields with <code>Nothing</code>/<code>null</code> values?  </p>