Haskell Language Data.Aeson - JSON in Haskell Optional Fields


Example

Sometimes, we want some fields in the JSON string to be optional. For example,

data Person = Person { firstName :: Text
                     , lastName  :: Text
                     , age       :: Maybe Int 
                     }

This can be achieved by

import Data.Aeson.TH

$(deriveJSON defaultOptions{omitNothingFields = True} ''Person)