{-# 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 Round = Round { _id :: Value, title :: Value } deriving (Show) instance ToJSON Round where toJSON Round{..} = object [ "i" .= _id, "t" .= title ] instance FromJSON Round where parseJSON = withObject "round" $ \o -> do _id <- o .: "id" title <- o .: "title" return Round{..} reduce :: [Round] -> Value -> [Round] reduce acc x = case (parseEither parseJSON x :: Either String Round) of (Left s) -> error s (Right v) -> v : acc parseRounds :: Either String [Value] -> [Round] parseRounds (Left x) = error x parseRounds (Right xs) = P.foldl reduce [] xs parseResult :: [Round] -> [(String, Value)] parseResult = P.foldl (\acc x -> (extractId x, title x) : acc) [] where extractId = BL8.unpack . encode . _id main :: IO () main = do src <- BL.readFile "./data/rounds.json" let teams = parseRounds (AE.eitherDecode src :: Either String [Value]) let encoded = encode $ HM.fromList $ parseResult teams BL8.putStrLn encoded BL8.putStrLn "Writing rounds.json" BL.writeFile "rounds.json" encoded