0

I have this data structure to take the result from a JSON in Haskell, with aeson

From this URL: http://fipeapi.appspot.com/api/1/carros/marcas.json

data Marca =
  Marca { keyM  :: String
         , idM   :: Int
         , fipe_nameM        :: String
         , nameM :: String
           } deriving Show

instance FromJSON Marca where 
    parseJSON (Object m) =
        Marca <$> m .: "key"
              <*> m .: "id"
              <*> m .: "fipe_name"
              <*> m .: "name"
    parseJSON _= mzero

This is working fine and i stored the result in a list of "Marca" named "ps"

main :: IO ()
main = do
    d <- (eitherDecode <$> getJSON) :: IO (Either String [Marca])
    case d of
        Left err -> putStrLn err 
        Right ps -> print ps
    return();

Now i have to create a list of Int from this "ps" with all "idM" from each "Marca", but i don't know how to do this, i try some things like:

Right ps -> let newlist = [idM x | x <- ps]
Right ps -> let newlist = [idM x | x <- [Marca]]

or

Right ps -> let newlist = map (idM x) ps
Right ps -> let newlist = map (idM x) [Marca]

But it did not work.

  • 2
    Let statements in Haskell require a body over which the let-binding will have scope. The syntax is `let = in `, ie, the `in` is mandatory. Try `let newlist = ... in print newlist`. – user2407038 Jun 06 '15 at 00:48
  • @user2407038 thanks, this solution worked. now i have to use this list to do a new consult in json with each element, there any way to make this list be global? – Julio Cesar Jun 06 '15 at 03:26

0 Answers0