using this Aeson example:
do result <- decode "{\"name\":\"Dave\",\"age\":2}"
flip parseMaybe result $ \obj -> do
age <- obj .: "age"
name <- obj .: "name"
return (name ++ ": " ++ show (age*2))
Just "Dave: 4"
The JSON string I'm trying to parse looks like this:
{ courses: [{id: 123, ...}, ...] }
I was wondering how to deal with lists, so I tried to cause type errors hoping the compiler would help out. But this compiled just fine.
getJsonCourseId' :: LB.ByteString -> Maybe Int
getJsonCourseId' body = do
result <- decode body
inners <- flip parseMaybe result $ \obj -> do
entities <- obj .: "courses"
return $ entities
flip parseMaybe inners $ \objs -> do
id <- objs .: "id"
return id
yes, it should.