You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

40 lines
1.3 KiB

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
import Data.Aeson as AE
import Data.Aeson.Types as AET
import Data.ByteString.Lazy as BL
import Data.ByteString.Lazy.Char8 as BL8
import Data.HashMap.Strict as HM
import Prelude as P
data Team = Team { _id :: Value, country :: Value } deriving (Show)
instance ToJSON Team where
toJSON Team{..} = object [ "i" .= _id, "c" .= country ]
instance FromJSON Team where
parseJSON = withObject "team" $ \o -> do
_id <- o .: "id"
country <- o .: "title"
return Team{..}
reduce :: [Team] -> Value -> [Team]
reduce acc x = case (parseEither parseJSON x :: Either String Team) of
(Left s) -> error s
(Right v) -> v : acc
parseTeams :: Either String [Value] -> [Team]
parseTeams (Left x) = error x
parseTeams (Right xs) = P.foldl reduce [] xs
parseResult :: [Team] -> [(String, Value)]
parseResult = P.foldl (\acc x -> (extractId x, country x) : acc) []
where
extractId = BL8.unpack . encode . _id
main :: IO ()
main = do
src <- BL.readFile "./data/teams.json"
let teams = parseTeams (AE.eitherDecode src :: Either String [Value])
BL.writeFile "teams.json" $ encode $ HM.fromList $ parseResult teams