If you really want to parse a single Movie
from a JSON array of movies, you can do something like this:
instance FromJSON Movie where
parseJSON (Object o) = do
movieValue <- head <$> o .: "movies"
Movie <$> movieValue .: "id" <*> movieValue .: "title"
parseJSON _ = mzero
But the safer route would be to parse a [Movie]
via newtype
wrapper:
main = print $ movieList <$> decode "{\"total\":1,\"movies\":[ {\"id\":\"771315522\",\"title\":\"Harry Potter and the Philosophers Stone (Wizard's Collection)\",\"posters\":{\"thumbnail\":\"http://content7.flixster.com/movie/11/16/66/11166609_mob.jpg\",\"profile\":\"http://content7.flixster.com/movie/11/16/66/11166609_pro.jpg\",\"detailed\":\"http://content7.flixster.com/movie/11/16/66/11166609_det.jpg\",\"original\":\"http://content7.flixster.com/movie/11/16/66/11166609_ori.jpg\"}}]}"
newtype MovieList = MovieList {movieList :: [Movie]}
instance FromJSON MovieList where
parseJSON (Object o) = MovieList <$> o .: "movies"
parseJSON _ = mzero
data Movie = Movie {id :: String, title :: String}
instance FromJSON Movie where
parseJSON (Object o) = Movie <$> o .: "id" <*> o .: "title"
parseJSON _ = mzero