I'm trying to parse the following JSON with aeson.
{
"data": [
{
"id": "34",
"type": "link",
"story": "foo"
},
{
"id": "35",
"type": "link",
"story": "bar"
}
]
}
Since there are a lot of field I'd like to ignore, it seems I should use GHC generics. But how to write a data type definition that uses Haskell keywords like data
and type
? The following of course gives: parse error on input `data'
data Feed = Feed {data :: [Post]}
deriving (Show, Generic)
data Post = Post {
id :: String,
type :: String,
story :: String
}
deriving (Show, Generic)
You can write your own
FromJSON
andToJSON
instances without relying on GHC.Generics. This also means that you can use different field names for the data representation and the JSON representation.Example instances for Post:
The same can be done for Feed. If you didn't have to ignore fields, you could also use
deriveJSON
fromData.Aeson.TH
, which takes a function to modify field names as it's first argument.