{-# 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 Prelude as P -- THIS "id":1 -- "key":null CHECKED, NULL -- THIS "round_id":1 round title join -- "pos":1 RANKING? NOT USING -- "group_id":null -- THIS "team1_id":45 -- THIS "team2_id":30 -- THIS "play_at":"2011-11-11 12:00:00.000000" -- "postponed":"f" CHECKED, NULL -- "play_at_v2":null CHECKED, NULL -- "play_at_v3":null CHECKED, NULL -- "ground_id":null CHECKED, NULL -- "city_id":null CHECKED, NULL -- "knockout":"f" -- "home":"t" -- THIS "score1":1 regular time -- THIS "score2":0 regular time -- THIS "score1et":null extended time -- THIS "score2et":null extended time -- THIS "score1p":null penalty time -- THIS "score2p":null penalty time -- -- "score1i":null CHECKED, NULL -- "score2i":null CHECKED, NULL -- "score1ii":null CHECKED, NULL -- "score2ii":null CHECKED, NULL -- "next_game_id":null CHECKED, NULL -- "prev_game_id":null CHECKED, NULL -- "winner":1 -- "winner90":1 -- "created_at":"2016-09-28 03:35:02.277055" -- "updated_at":"2016-09-28 03:35:02.277055" data Game = Game { round_id :: Value, team1_id :: Value, team2_id :: Value, playAt :: Value, score1 :: Value, score2 :: Value, score1et :: Value, score2et :: Value, score1p :: Value, score2p :: Value } deriving (Show) instance ToJSON Game where toJSON Game{..} = object [ "rId" .= round_id, "t1" .= team1_id, "t2" .= team2_id, "ts" .= playAt, "s1" .= score1, "s2" .= score2, "s1e" .= score1et, "s2e" .= score2et, "s1p" .= score1p, "s2p" .= score2p ] instance FromJSON Game where parseJSON = withObject "game" $ \o -> do round_id <- o .: "round_id" team1_id <- o .: "team1_id" team2_id <- o .: "team2_id" playAt <- o .: "play_at" score1 <- o .: "score1" score2 <- o .: "score2" score1et <- o .: "score1et" score2et <- o .: "score2et" score1p <- o .: "score1p" score2p <- o .: "score2p" return Game{..} reduce :: [Game] -> Value -> [Game] reduce acc x = case (parseEither parseJSON x :: Either String Game) of (Left s) -> error s (Right v) -> v : acc parseGames :: Either String [Value] -> [Game] parseGames (Left x) = error x parseGames (Right xs) = P.foldl reduce [] xs main :: IO () main = do src <- BL.readFile "./data/games.json" let games = parseGames (AE.eitherDecode src :: Either String [Value]) let encoded = encode games BL8.putStrLn encoded BL8.putStrLn "Writing games.json" BL.writeFile "games.json" encoded