I have trouble parsing the Tag in the following JSON structure. The parser works only when i declare it to be tags :: !Array
It fails when i declare it as tags :: [Tag]
Why?
{
"response": {
"status": "ok",
"results": [
{
"type": "article",
"fields": {
"wordcount": "497"
},
"tags": [
{
"id": "profile/barryglendenning"
}
]
}
]
}
}
data Field = Field{
wordcount :: Int
} deriving (Show)
instance FromJSON Field where
parseJSON (Object o) = Field <$> (o .: "wordcount")
parseJSON _ = mzero
data Tag = Tag{
id :: Text
} deriving (Show)
instance FromJSON Tag where
parseJSON (Object o) = Tag <$> (o .: "id")
parseJSON _ = mzero
data SearchResult = SearchResult {
type:: Text,
field :: Field,
tags :: !Array
} deriving (Show)
instance FromJSON SearchResult where
parseJSON (Object o) = do
let t1 = o .: "type"
let t2 = o .: "fields"
let t3 = o .: "tags"
SearchResult <$> t1 <*> t2 <*> t3
parseJSON _ = mzero
data ContentrResult = ContentrResult {
results :: [SearchResult],
status :: Text
} deriving (Show)
instance FromJSON ContentrResult where
parseJSON (Object o) = do
r <- o .: "response"
ContentrResult <$> r .: "results"
<*> r .: "status"
parseJSON _ = mzero
id
, because then you would have an ambiguity with the prelude. – Jean-Baptiste Potonniertype
is a syntax error – Jean-Baptiste PotonnierInt
when it is a string in json. I wish aeson could provide Either instead of Maybe. – Jean-Baptiste Potonnier